Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update coverage utils #234

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions Networking/Tests/NetworkingTests/PeerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,12 @@ struct PeerTests {
presistentStreamHandler: MockPresentStreamHandler(),
ephemeralStreamHandler: MockEphemeralStreamHandler(),
serverSettings: .defaultSettings,
clientSettings: .defaultSettings
clientSettings: .defaultSettings,
peerSettings: PeerSettings(maxBuilderConnections: 3)
)
)
// Create 30 peer nodes
for _ in 0 ..< 30 {
// Create 5 peer nodes
for _ in 0 ..< 5 {
let handler = MockPresentStreamHandler()
handlers.append(handler)
let peer = try Peer(
Expand All @@ -160,20 +161,20 @@ struct PeerTests {
}

// Make some connections
for i in 0 ..< 30 {
for i in 0 ..< 5 {
let peer = peers[i]
let con = try peer.connect(to: centerPeer.listenAddress(), role: .builder)
try await con.ready()
}
// Simulate close connections 5~8s
try? await Task.sleep(for: .milliseconds(8000))
centerPeer.broadcast(kind: .uniqueA, message: .init(kind: .uniqueA, data: Data("connection rotation strategy".utf8)))
// Simulate close connections 1~3s
try? await Task.sleep(for: .milliseconds(1000))
centerPeer.broadcast(kind: .uniqueA, message: .init(kind: .uniqueA, data: Data("connection rotation strategy".utf8)))
try? await Task.sleep(for: .milliseconds(100))
var receivedCount = 0
for handler in handlers {
receivedCount += await handler.receivedData.count
}
#expect(receivedCount == PeerSettings.defaultSettings.maxBuilderConnections)
#expect(receivedCount == 3)
}

@Test
Expand Down
7 changes: 1 addition & 6 deletions Utils/Sources/Utils/JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import Foundation

public indirect enum JSON: Codable, Equatable {
public indirect enum JSON: Codable, Equatable, Sendable {
case dictionary([String: JSON])
case array([JSON])
case string(String)
Expand Down Expand Up @@ -155,11 +155,6 @@ extension JSON {
stringValue = value.description
}

init(_ value: String) {
intValue = nil
stringValue = value
}

init?(intValue: Int) {
self.init(intValue)
}
Expand Down
62 changes: 61 additions & 1 deletion Utils/Tests/UtilsTests/ConfigLimitedSizeArrayTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ import Testing

@testable import Utils

struct MinLengthNegated: ReadInt {
typealias TConfig = Int

static func read(config _: Int) -> Int {
-1
}
}

struct MinLength3: ReadInt {
typealias TConfig = Int

Expand All @@ -20,13 +28,24 @@ struct MaxLength5: ReadInt {
}
}

struct MaxLength8: ReadInt {
typealias TConfig = Int

static func read(config _: Int) -> Int {
8
}
}

struct ConfigLimitedSizeArrayTests {
@Test func initWithDefaultValue() throws {
let config = 0
let defaultValue = 1
let array = try ConfigLimitedSizeArray<Int, MinLength3, MaxLength5>(config: config, defaultValue: defaultValue)
var array = try ConfigLimitedSizeArray<Int, MinLength3, MaxLength5>(config: config, defaultValue: defaultValue)
#expect(array.array == [1, 1, 1])
#expect(array.count == 3)
#expect(array[0] == 1)
array[0] = 0
#expect(array[0] != 1)
}

@Test func initWithArrayWithinBounds() throws {
Expand Down Expand Up @@ -141,4 +160,45 @@ struct ConfigLimitedSizeArrayTests {
let decoded = try JamDecoder.decode(ConfigLimitedSizeArray<Int, MinLength3, MaxLength5>.self, from: encoded, withConfig: config)
#expect(decoded == array)
}

@Test func throwLength() throws {
#expect(throws: Error.self) {
_ = try ConfigLimitedSizeArray<Int, MinLengthNegated, MaxLength5>(config: 0, array: [1, 2, 3])
}
#expect(throws: Error.self) {
_ = try ConfigLimitedSizeArray<Int, MaxLength5, MinLength3>(config: 0, array: [1, 2, 3])
}
}

@Test func randomAccessCollection() throws {
let value = try ConfigLimitedSizeArray<Int, MinLength3, MaxLength8>(config: 0, array: [1, 2, 3, 4, 5, 6, 7, 8])
#expect(value.startIndex == 0)
#expect(value.endIndex == 8)

var idx = value.startIndex
value.formIndex(after: &idx)
#expect(idx == 1)

value.formIndex(before: &idx)
#expect(idx == 0)

let dist = value.distance(from: 0, to: 7)
#expect(dist == 7)

let indexForward = value.index(0, offsetBy: 3)
#expect(indexForward == 3)

let indexWithinLimit = value.index(0, offsetBy: 3, limitedBy: 5)
#expect(indexWithinLimit == 3)
#expect(value.index(after: indexWithinLimit!) == 4)
#expect(value.index(before: indexWithinLimit!) == 2)
#expect(value.index(from: indexWithinLimit!) == 3)
}

@Test func description() throws {
let config = 0
let array = try ConfigLimitedSizeArray<Int, MinLength3, MaxLength5>(config: config, array: [1, 2, 3, 4, 5])
#expect(array.description == "[1, 2, 3, 4, 5]")
#expect(array.debugDescription == "ConfigLimitedSizeArray<Int, 3, 5>([1, 2, 3, 4, 5])")
}
}
40 changes: 40 additions & 0 deletions Utils/Tests/UtilsTests/ConfigSizeBitStringTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,44 @@ struct ConfigSizeBitStringTests {
value[19] = true
#expect(value == value3)
}

@Test func encodedSizeTests() throws {
let data = Data([0b1011_0101, 0b1100_0101, 0b0000_0110])
let length = 20
let value = try ConfigSizeBitString<ReadIntValue>(config: length, data: data)

#expect(value.encodedSize == data.count)
#expect(ConfigSizeBitString<ReadIntValue>.encodeedSizeHint == nil)
}

@Test func randomAccessCollection() throws {
var value = ConfigSizeBitString<ReadIntValue>(config: 8)
let result = value.withPtr { ptr in
ptr.reduce(0, +)
}
#expect(result == 0)

#expect(value.startIndex == 0)
#expect(value.endIndex == 8)

value[7] = true
let collected = Array(value)
#expect(collected == [false, false, false, false, false, false, false, true])

var idx = value.startIndex
value.formIndex(after: &idx)
#expect(idx == 1)

value.formIndex(before: &idx)
#expect(idx == 0)

let dist = value.distance(from: 0, to: 7)
#expect(dist == 7)

let indexForward = value.index(0, offsetBy: 3)
#expect(indexForward == 3)

let indexWithinLimit = value.index(0, offsetBy: 3, limitedBy: 5)
#expect(indexWithinLimit == 3)
}
}
22 changes: 22 additions & 0 deletions Utils/Tests/UtilsTests/ConstValueTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Testing

@testable import Utils

struct ConstIntTests {
@Test
func constIntValues() {
#expect(ConstInt0.value == 0)
#expect(ConstInt1.value == 1)
#expect(ConstInt2.value == 2)
#expect(ConstInt3.value == 3)
#expect(ConstIntMax.value == Int.max)
#expect(ConstInt32.value == 32)
#expect(ConstInt48.value == 48)
#expect(ConstInt64.value == 64)
#expect(ConstUInt96.value == 96)
#expect(ConstUInt128.value == 128)
#expect(ConstUInt144.value == 144)
#expect(ConstUInt384.value == 384)
#expect(ConstUInt784.value == 784)
}
}
155 changes: 155 additions & 0 deletions Utils/Tests/UtilsTests/EitherTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import Codec
import Foundation
import Testing

@testable import Utils

struct EitherTests {
struct EncodedString: EncodedSize {
let value: String

var encodedSize: Int {
value.utf8.count
}

Check warning on line 13 in Utils/Tests/UtilsTests/EitherTests.swift

View check run for this annotation

Codecov / codecov/patch

Utils/Tests/UtilsTests/EitherTests.swift#L11-L13

Added lines #L11 - L13 were not covered by tests

static var encodeedSizeHint: Int? {
nil
}
}

struct EncodedInt: EncodedSize {
let value: Int

var encodedSize: Int {
MemoryLayout<Int>.size
}

static var encodeedSizeHint: Int? {
MemoryLayout<Int>.size
}
}

typealias MyEither = Either<EncodedString, EncodedInt>
typealias MyIntEither = Either<EncodedInt, EncodedInt>

@Test(arguments: [
Data([0x02]),
])
func throwsOnUnknownVariant(data: Data) {
#expect(throws: Error.self) {
_ = try JamDecoder.decode(Either<String, Int>.self, from: data)
}
}

func encodedSizeForLeft() {
let either = MyEither.left(EncodedString(value: "Hi"))
#expect(either.encodedSize == 3)
}

Check warning on line 47 in Utils/Tests/UtilsTests/EitherTests.swift

View check run for this annotation

Codecov / codecov/patch

Utils/Tests/UtilsTests/EitherTests.swift#L44-L47

Added lines #L44 - L47 were not covered by tests

@Test
func encodedSizeForRight() {
let either = MyEither.right(EncodedInt(value: 42))
#expect(either.encodedSize == 9)
}

@Test
func encodeedSizeHint() {
let hint = MyEither.encodeedSizeHint
#expect(hint == nil)
let IntHint = MyIntEither.encodeedSizeHint
#expect(IntHint == MemoryLayout<Int>.size * 2 + 1)
}

@Test(arguments: [
Either<String, Int>.left("Hello"),
Either<String, Int>.right(42),
])
func testAccessors(either: Either<String, Int>) {
if let left = either.left {
#expect(left == "Hello")
#expect(either.right == nil)
} else if let right = either.right {
#expect(right == 42)
#expect(either.left == nil)
}
}

@Test(arguments: [
(Either<String, Int>.left("Test"), Either<String, Int>.left("Test"), true),
(Either<String, Int>.right(100), Either<String, Int>.right(100), true),
(Either<String, Int>.left("A"), Either<String, Int>.right(100), false),
(Either<String, Int>.right(42), Either<String, Int>.right(100), false)
])
func equality(lhs: Either<String, Int>, rhs: Either<String, Int>, expected: Bool) {
#expect((lhs == rhs) == expected)
}

@Test(arguments: [
Either<String, Int>.left("Left Value"),
Either<String, Int>.right(123)
])
func JSONCoding(either: Either<String, Int>) throws {
let encoder = JSONEncoder()
let data = try encoder.encode(either)
let decoder = JSONDecoder()
let decoded = try decoder.decode(Either<String, Int>.self, from: data)
#expect(decoded == either)
}

@Test(arguments: [
Either<String, Int>.left("Jam Codec Left"),
Either<String, Int>.right(456),
])
func JamCoding(either: Either<String, Int>) throws {
let encoded = try JamEncoder.encode(either)
let decoder = JamDecoder(data: encoded)
let decoded = try decoder.decode(Either<String, Int>.self)
#expect(decoded == either)
}

@Test(arguments: [
(Either<String, Int>.left("Describe Me"), "Left(Describe Me)"),
(Either<String, Int>.right(789), "Right(789)"),
])
func description(either: Either<String, Int>, expected: String) {
#expect(either.description == expected)
}

@Test(arguments: [
Either<Int, String>.left(42),
Either<Int, String>.right("hello"),
])
func maybeEitherInit(either: Either<Int, String>) {
let maybe = MaybeEither(either)
#expect(maybe.value == either)
}

@Test(arguments: [
42,
-1,
1000,
])
func maybeEitherInitLeft(left: Int) {
let maybe = MaybeEither<Int, String>(left: left)
#expect(maybe.value == .left(left))
}

@Test(arguments: [
"hello",
"world",
"",
])
func maybeEitherInitRight(right: String) {
let maybe = MaybeEither<Int, String>(right: right)
#expect(maybe.value == .right(right))
}

@Test(arguments: [
Either<Int, Int>.left(42),
Either<Int, Int>.right(99),
])
func testMaybeEitherUnwrapped(either: Either<Int, Int>) {
let maybe = MaybeEither(either)
#expect(maybe.unwrapped == (either == .left(42) ? 42 : 99))
}
}
Loading