Skip to content

Commit

Permalink
Release 2.5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
hdrsl committed Sep 28, 2022
1 parent 6bf9f11 commit 268987e
Show file tree
Hide file tree
Showing 46 changed files with 1,098 additions and 984 deletions.
44 changes: 44 additions & 0 deletions BlueGeckoTests/Helpers/AsciiStringConverterTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// AsciiStringConverterTest.swift
// BlueGeckoTests
//
// Created by Hubert Drogosz on 29/07/2022.
// Copyright © 2022 SiliconLabs. All rights reserved.
//

import Foundation
import Quick
import Nimble
@testable import BlueGecko

class AsciiStringConverterTest : QuickSpec {
override func spec() {
let fieldModel = SILBluetoothFieldModel()
let converter = AsciiStringConverter()

it("dataToString should change non-ascii characters to replacement character") {
let nonAsciiText = "Aąb"
let d = nonAsciiText.data(using: .utf8)!

expect(try? converter.dataToString(d, fieldModel: fieldModel)
.get()
.unicodeScalars.map { $0.value }) == [0x41, 0xFFFD, 0xFFFD, 0x62]

}
it("stringToData should fail with wrong characters") {
let nonAsciiText = "Aąb"

expect(try? converter.stringToData(nonAsciiText, fieldModel: fieldModel).get()) == nil
}

it("should return the same string after stringToData and dataToString") {
let inputString : String = "Abcdefghijklmn opqrstuwxyz\n"

let result = try! converter.stringToData(inputString, fieldModel: fieldModel)
.flatMap({ converter.dataToString($0, fieldModel: fieldModel) })
.get()

expect(result) == inputString
}
}
}
168 changes: 168 additions & 0 deletions BlueGeckoTests/Helpers/IntegerValueConverterTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
//
// IntegerValueConverterTest.swift
// BlueGeckoTests
//
// Created by Hubert Drogosz on 05/08/2022.
// Copyright © 2022 SiliconLabs. All rights reserved.
//

import Foundation
import Quick
import Nimble
@testable import BlueGecko

class IntegerValueConverterTest : QuickSpec {
override func spec() {

var fieldModel = SILBluetoothFieldModel()

beforeEach {
fieldModel = SILBluetoothFieldModel()
fieldModel.invertedBytesOrder = false
fieldModel.decimalExponent = 0
fieldModel.multiplier = 1
}

describe("UInt8") {
let converter = IntegerValueConverter<UInt8>(resultBitLength: 8)

it("should return correct string for data") {
let inputData = Data(bytes: [123, 64])
expect(try? converter.dataToString(inputData, fieldModel: fieldModel).get()) == "123"
}
it("should return the same string after stringToData and dataToString") {
let inputString = "254"
let result = try? converter.stringToData(inputString, fieldModel: fieldModel)
.flatMap({ converter.dataToString($0, fieldModel: fieldModel) })
.get()

expect(result) == inputString
}
}

describe("Int8") {
let converter = IntegerValueConverter<Int8>(resultBitLength: 8)

it("should return correct string for data") {
let inputData = Data(bytes: [254, 64])
expect(try? converter.dataToString(inputData, fieldModel: fieldModel).get()) == "-2"
}
it("should return the same string after stringToData and dataToString") {
let inputString = "-120"
let result = try? converter.stringToData(inputString, fieldModel: fieldModel)
.flatMap({ converter.dataToString($0, fieldModel: fieldModel) })
.get()

expect(result) == inputString
}
}

describe("UInt16") {
let converter = IntegerValueConverter<UInt16>(resultBitLength: 16)

it("should return correct value") {
fieldModel.decimalExponent = 2
let inputData = Data(bytes: [0b11010100,0b10101001])
expect(try? converter.dataToString(inputData, fieldModel: fieldModel).get()) == "4347600"
}

it("should return correctValue with invertedBytesOrder") {
fieldModel.invertedBytesOrder = true
fieldModel.multiplier = -1
let inputData = Data(bytes: [0b10101001, 0b11010100])
expect(try? converter.dataToString(inputData, fieldModel: fieldModel).get()) == "-43476"
}

it("should return the same string after stringToData and dataToString") {
fieldModel.decimalExponent = 2
let inputString = "64500"
let result = try? converter.stringToData(inputString, fieldModel: fieldModel)
.flatMap({ converter.dataToString($0, fieldModel: fieldModel) })
.get()

expect(result) == inputString
}
}

describe("UInt48") {
let converter = IntegerValueConverter<UInt64>(resultBitLength: 48)

it("should return correct value") {
let inputData = Data(bytes: withUnsafeBytes(of: 0b101101111010111110011111001110000110110100110110.littleEndian, Array.init))
expect(try? converter.dataToString(inputData, fieldModel: fieldModel).get()) == "201964918435126"
}

it("should return correct value with all modifiers") {
fieldModel.multiplier = 200
fieldModel.decimalExponent = -2
fieldModel.invertedBytesOrder = true

let inputData = Data(bytes: withUnsafeBytes(of: 0b101101111010111110011111001110000110110100110110.bigEndian, Array.init))
expect(try? converter.dataToString(inputData, fieldModel: fieldModel).get()) == "403929836870252"
}

it("should return the same string after stringToData and dataToString") {
let inputString = "281 471 509 472 188"
let result = try? converter.stringToData(inputString, fieldModel: fieldModel)
.flatMap({ converter.dataToString($0, fieldModel: fieldModel) })
.get()

expect(result) == inputString.replacingOccurrences(of: " ", with: "")
}

it("should return the same string after stringToData and dataToString with modifiers") {
fieldModel.multiplier = 30
fieldModel.decimalExponent = -1
fieldModel.invertedBytesOrder = true

let inputString = "281 471 509 472 100"
let halfResult = converter.stringToData(inputString, fieldModel: fieldModel)
let result = try? halfResult.flatMap({ converter.dataToString($0, fieldModel: fieldModel) })
.get()

expect(result) == inputString.replacingOccurrences(of: " ", with: "")
}
}

describe("Int48") {
let converter = IntegerValueConverter<Int64>(resultBitLength: 48)

it("should return correct value") {
let inputData = Data(bytes: withUnsafeBytes(of: 0b101000110100001101110001010000011101001011001010.littleEndian, Array.init))
expect(try? converter.dataToString(inputData, fieldModel: fieldModel).get()) == "-101964918435126"
}

it("should return correct value with all modifiers") {
fieldModel.multiplier = 200
fieldModel.decimalExponent = -2
fieldModel.invertedBytesOrder = true

let inputData = Data(bytes: withUnsafeBytes(of: 0b101000110100001101110001010000011101001011001010.bigEndian, Array.init))
expect(try? converter.dataToString(inputData, fieldModel: fieldModel).get()) == "-203929836870252"
}

it("should return the same string after stringToData and dataToString") {
let inputString = "-10 1 9 6 4 9 1 8 4 3 5 126"
let result = try? converter.stringToData(inputString, fieldModel: fieldModel)
.flatMap({ converter.dataToString($0, fieldModel: fieldModel) })
.get()

expect(result) == inputString.replacingOccurrences(of: " ", with: "")
}

it("should return the same string after stringToData and dataToString with modifiers") {
fieldModel.multiplier = 30
fieldModel.decimalExponent = -1
fieldModel.invertedBytesOrder = true

let inputString = "281 471 509 472 100"
let halfResult = converter.stringToData(inputString, fieldModel: fieldModel)
let result = try? halfResult.flatMap({ converter.dataToString($0, fieldModel: fieldModel) })
.get()

expect(result) == inputString.replacingOccurrences(of: " ", with: "")
}
}
}
}

80 changes: 17 additions & 63 deletions BlueGeckoTests/Helpers/SILValueResolverTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
#import "SILCharacteristicFieldValueResolver.h"
#import "SILBluetoothFieldModel.h"

@interface SILCharacteristicFieldValueResolver()
- (SInt64)sintCasterForSint:(SInt64)result originalSize:(int)originalSize castSize:(int)castSize;
@end

@interface SILCharacteristicFieldValueResolver(TestPrivate)
- (NSData*)dataForValueString:(NSString *)dataString asFormat:(NSString *)format;
Expand Down Expand Up @@ -465,9 +462,9 @@ - (void)testWriteSint24_Negative {

NSData *writtenData = [[SILCharacteristicFieldValueResolver sharedResolver] dataForValueString:@"-838445" asFormat:kS24Format];
[writtenData getBytes:&written length:sizeof(sizeBuffer)];
written = (SInt32)[[SILCharacteristicFieldValueResolver sharedResolver] sintCasterForSint:written originalSize:24 castSize:32];

XCTAssertEqual(target, written);

XCTAssertEqual(target & 0x00FFFFFF, written & 0x00FFFFFF);
}

- (void)testWriteSint24_Positive {
Expand All @@ -477,9 +474,8 @@ - (void)testWriteSint24_Positive {

NSData *writtenData = [[SILCharacteristicFieldValueResolver sharedResolver] dataForValueString:@"638200" asFormat:kS24Format];
[writtenData getBytes:&written length:sizeof(sizeBuffer)];
written = (SInt32)[[SILCharacteristicFieldValueResolver sharedResolver] sintCasterForSint:written originalSize:24 castSize:32];

XCTAssertEqual(target, written);
XCTAssertEqual(target & 0x00FFFFFF, written & 0x00FFFFFF);
}

#pragma mark - sint32
Expand Down Expand Up @@ -533,9 +529,8 @@ - (void)testWriteSint48_Negative {

NSData *writtenData = [[SILCharacteristicFieldValueResolver sharedResolver] dataForValueString:@"-92740304304534" asFormat:kS48Format];
[writtenData getBytes:&written length:sizeof(sizeBuffer)];
written = (SInt64)[[SILCharacteristicFieldValueResolver sharedResolver] sintCasterForSint:written originalSize:48 castSize:64];

XCTAssertEqual(target, written);
XCTAssertEqual(target & 0x0000FFFFFFFFFFFF, written & 0x0000FFFFFFFFFFFF);
}

- (void)testWriteSint48_Positive {
Expand All @@ -545,9 +540,8 @@ - (void)testWriteSint48_Positive {

NSData *writtenData = [[SILCharacteristicFieldValueResolver sharedResolver] dataForValueString:@"29374398573359" asFormat:kS48Format];
[writtenData getBytes:&written length:sizeof(sizeBuffer)];
written = (SInt64)[[SILCharacteristicFieldValueResolver sharedResolver] sintCasterForSint:written originalSize:48 castSize:64];

XCTAssertEqual(target, written);
XCTAssertEqual(target & 0x0000FFFFFFFFFFFF, written & 0x0000FFFFFFFFFFFF);
}

#pragma mark - sint 64
Expand Down Expand Up @@ -635,6 +629,16 @@ - (void)testWriteFloat_PositiveDecimal {
XCTAssertTrue([writtenData isEqualToData:targetData]);
}

- (void)testFloat {
uint16_t bytes[2] = {0x016C, 0xFF00};
XCTAssertTrue([self assertBytes:bytes ofLength:4 areEqualToExpected:@"36.4" forFormat:kFloatFormat]);
}

- (void)testFloatNegative {
uint16_t bytes[2] = {0x718C, 0xFFFF};
XCTAssertTrue([self assertBytes:bytes ofLength:4 areEqualToExpected:@"-3646.8" forFormat:kFloatFormat]);
}

#pragma mark - SFLOAT

- (void)testSFloat_Positive {
Expand Down Expand Up @@ -664,12 +668,12 @@ - (void)testSFloat_NegativeDecimal {

- (void)testSFloat_BorderValue1 {
uint16_t bytes[1] = {0x0800};
XCTAssertTrue([self assertBytes:bytes ofLength:2 areEqualToExpected:@"2047" forFormat:kSFloatFormat]);
XCTAssertTrue([self assertBytes:bytes ofLength:2 areEqualToExpected:@"-2048" forFormat:kSFloatFormat]);
}

- (void)testSFLoat_BorderValue2 {
uint16_t bytes[1] = {0x0801};
XCTAssertTrue([self assertBytes:bytes ofLength:2 areEqualToExpected:@"2047" forFormat:kSFloatFormat]);
XCTAssertTrue([self assertBytes:bytes ofLength:2 areEqualToExpected:@"-2047" forFormat:kSFloatFormat]);
}

- (void)testWriteSFloatString_Positive {
Expand Down Expand Up @@ -745,10 +749,6 @@ - (void)testWriteUtf16 {
XCTAssert([written isEqualToString:target]);
}

#pragma mark - reg-cert-data-list

#pragma mark - variable

#pragma mark - Hex String encoding

- (void)testDataForHexSuccess {
Expand Down Expand Up @@ -839,52 +839,6 @@ - (void)testDataForDecimalFailure_NoSpace {
XCTAssertNil(stringData);
}

#pragma mark - Utility Tests

- (void)testExponentPositive {
Float32 number = 535000.0f;
int exponent = [[SILCharacteristicFieldValueResolver sharedResolver] exponentOfIntegerBaseForFloat:number];

XCTAssertTrue(exponent == 3);
}

- (void)testExponentLessThanOne {
Float32 number = 0.052f;
int exponent = [[SILCharacteristicFieldValueResolver sharedResolver] exponentOfIntegerBaseForFloat:number];

XCTAssertTrue(exponent == -3);
}

- (void)testExponentZero {
int exponent = [[SILCharacteristicFieldValueResolver sharedResolver] exponentOfIntegerBaseForFloat:0];
XCTAssertTrue(exponent == 0);
}

- (void)testExponentOne {
int exponent = [[SILCharacteristicFieldValueResolver sharedResolver] exponentOfIntegerBaseForFloat:1];
XCTAssertTrue(exponent == 0);
}

- (void)testExponentTen {
int exponent = [[SILCharacteristicFieldValueResolver sharedResolver] exponentOfIntegerBaseForFloat:10];
XCTAssertTrue(exponent == 1);
}

- (void)testExponentTwelve {
int exponent = [[SILCharacteristicFieldValueResolver sharedResolver] exponentOfIntegerBaseForFloat:12];
XCTAssertTrue(exponent == 0);
}

- (void)testExponentTwenty {
int exponent = [[SILCharacteristicFieldValueResolver sharedResolver] exponentOfIntegerBaseForFloat:20];
XCTAssertTrue(exponent == 1);
}

- (void)testExponentTwentyThree {
int exponent = [[SILCharacteristicFieldValueResolver sharedResolver] exponentOfIntegerBaseForFloat:23];
XCTAssertTrue(exponent == 0);
}

#pragma mark - Test Helpers

- (BOOL)assertBytes:(const void *)bytes ofLength:(int)length areEqualToExpected:(NSString *)expectedValue forFormat:(NSString *)format {
Expand Down
Loading

0 comments on commit 268987e

Please sign in to comment.