2016-11-19 00:27:08 +00:00
|
|
|
//
|
|
|
|
// ArrayBuilderTests.m
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 19/11/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2016-12-18 02:13:57 +00:00
|
|
|
#import <XCTest/XCTest.h>
|
|
|
|
|
2016-11-19 00:27:08 +00:00
|
|
|
#include "ArrayBuilder.hpp"
|
|
|
|
|
|
|
|
static NSData *inputData, *outputData;
|
|
|
|
|
|
|
|
static void setData(bool is_input, uint8_t *data, size_t size)
|
|
|
|
{
|
|
|
|
NSData *dataObject = [NSData dataWithBytes:data length:size];
|
|
|
|
if(is_input) inputData = dataObject; else outputData = dataObject;
|
|
|
|
}
|
|
|
|
|
2016-12-18 02:13:57 +00:00
|
|
|
@interface ArrayBuilderTests : XCTestCase
|
|
|
|
@end
|
|
|
|
|
2016-11-19 00:27:08 +00:00
|
|
|
@implementation ArrayBuilderTests
|
|
|
|
|
|
|
|
+ (void)setUp
|
|
|
|
{
|
|
|
|
inputData = nil;
|
|
|
|
outputData = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)assertMonotonicForInputSize:(size_t)inputSize outputSize:(size_t)outputSize
|
|
|
|
{
|
|
|
|
XCTAssert(inputData != nil, @"Should have received some input data");
|
|
|
|
XCTAssert(outputData != nil, @"Should have received some output data");
|
|
|
|
|
2016-11-19 11:48:16 +00:00
|
|
|
XCTAssert(inputData.length == inputSize, @"Input data should be %lu bytes long, was %lu", inputSize, (unsigned long)inputData.length);
|
|
|
|
XCTAssert(outputData.length == outputSize, @"Output data should be %lu bytes long, was %lu", outputSize, (unsigned long)outputData.length);
|
2016-11-19 00:27:08 +00:00
|
|
|
|
2016-12-03 21:06:15 +00:00
|
|
|
if(inputData.length == inputSize && outputData.length == outputSize)
|
|
|
|
{
|
|
|
|
uint8_t *input = (uint8_t *)inputData.bytes;
|
|
|
|
uint8_t *output = (uint8_t *)outputData.bytes;
|
|
|
|
|
|
|
|
for(int c = 0; c < inputSize; c++) XCTAssert(input[c] == c, @"Input item %d should be %d, was %d", c, c, input[c]);
|
|
|
|
for(int c = 0; c < outputSize; c++) XCTAssert(output[c] == c + 0x80, @"Output item %d should be %d, was %d", c, c+0x80, output[c]);
|
|
|
|
}
|
2016-11-19 00:27:08 +00:00
|
|
|
}
|
|
|
|
|
2016-12-17 22:05:49 +00:00
|
|
|
- (std::function<void(uint8_t *input, size_t input_size, uint8_t *output, size_t output_size)>)emptyFlushFunction
|
|
|
|
{
|
|
|
|
return [=] (uint8_t *input, size_t input_size, uint8_t *output, size_t output_size) {};
|
|
|
|
}
|
|
|
|
|
2016-11-19 00:27:08 +00:00
|
|
|
- (void)testSingleWriteSingleFlush
|
|
|
|
{
|
|
|
|
Outputs::CRT::ArrayBuilder arrayBuilder(200, 100, setData);
|
|
|
|
|
|
|
|
uint8_t *input = arrayBuilder.get_input_storage(5);
|
|
|
|
uint8_t *output = arrayBuilder.get_output_storage(3);
|
|
|
|
|
|
|
|
for(int c = 0; c < 5; c++) input[c] = c;
|
|
|
|
for(int c = 0; c < 3; c++) output[c] = c + 0x80;
|
|
|
|
|
2016-12-17 22:05:49 +00:00
|
|
|
arrayBuilder.flush(self.emptyFlushFunction);
|
2016-11-19 00:27:08 +00:00
|
|
|
arrayBuilder.submit();
|
|
|
|
|
|
|
|
[self assertMonotonicForInputSize:5 outputSize:3];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testDoubleWriteSingleFlush
|
|
|
|
{
|
|
|
|
Outputs::CRT::ArrayBuilder arrayBuilder(200, 100, setData);
|
|
|
|
uint8_t *input;
|
|
|
|
uint8_t *output;
|
|
|
|
|
|
|
|
input = arrayBuilder.get_input_storage(2);
|
|
|
|
output = arrayBuilder.get_output_storage(2);
|
|
|
|
|
|
|
|
for(int c = 0; c < 2; c++) input[c] = c;
|
|
|
|
for(int c = 0; c < 2; c++) output[c] = c + 0x80;
|
|
|
|
|
|
|
|
input = arrayBuilder.get_input_storage(2);
|
|
|
|
output = arrayBuilder.get_output_storage(2);
|
|
|
|
|
|
|
|
for(int c = 0; c < 2; c++) input[c] = c+2;
|
|
|
|
for(int c = 0; c < 2; c++) output[c] = c+2 + 0x80;
|
|
|
|
|
2016-12-17 22:05:49 +00:00
|
|
|
arrayBuilder.flush(self.emptyFlushFunction);
|
2016-11-19 00:27:08 +00:00
|
|
|
arrayBuilder.submit();
|
|
|
|
|
|
|
|
[self assertMonotonicForInputSize:4 outputSize:4];
|
|
|
|
}
|
|
|
|
|
2016-11-19 00:59:21 +00:00
|
|
|
- (void)testSubmitWithoutFlush
|
|
|
|
{
|
|
|
|
Outputs::CRT::ArrayBuilder arrayBuilder(200, 100, setData);
|
|
|
|
|
|
|
|
arrayBuilder.get_input_storage(5);
|
|
|
|
arrayBuilder.get_input_storage(8);
|
|
|
|
arrayBuilder.get_output_storage(6);
|
|
|
|
arrayBuilder.get_input_storage(12);
|
|
|
|
arrayBuilder.get_output_storage(3);
|
|
|
|
|
|
|
|
arrayBuilder.submit();
|
|
|
|
|
|
|
|
XCTAssert(inputData.length == 0, @"No input data should have been received; %lu bytes were received", (unsigned long)inputData.length);
|
|
|
|
XCTAssert(outputData.length == 0, @"No output data should have been received; %lu bytes were received", (unsigned long)outputData.length);
|
|
|
|
|
2016-12-17 22:05:49 +00:00
|
|
|
arrayBuilder.flush(self.emptyFlushFunction);
|
2016-11-19 00:59:21 +00:00
|
|
|
arrayBuilder.submit();
|
|
|
|
|
|
|
|
XCTAssert(inputData.length == 25, @"All input data should have been received; %lu bytes were received", (unsigned long)inputData.length);
|
|
|
|
XCTAssert(outputData.length == 9, @"All output data should have been received; %lu bytes were received", (unsigned long)outputData.length);
|
|
|
|
}
|
|
|
|
|
2016-11-19 11:48:16 +00:00
|
|
|
- (void)testSubmitContinuity
|
|
|
|
{
|
|
|
|
Outputs::CRT::ArrayBuilder arrayBuilder(200, 100, setData);
|
|
|
|
|
|
|
|
arrayBuilder.get_input_storage(5);
|
|
|
|
arrayBuilder.get_output_storage(5);
|
|
|
|
|
2016-12-17 22:05:49 +00:00
|
|
|
arrayBuilder.flush(self.emptyFlushFunction);
|
2016-11-19 11:48:16 +00:00
|
|
|
|
|
|
|
uint8_t *input = arrayBuilder.get_input_storage(5);
|
|
|
|
uint8_t *output = arrayBuilder.get_output_storage(5);
|
|
|
|
|
|
|
|
arrayBuilder.submit();
|
|
|
|
|
|
|
|
for(int c = 0; c < 5; c++) input[c] = c;
|
|
|
|
for(int c = 0; c < 5; c++) output[c] = c + 0x80;
|
|
|
|
|
2016-12-17 22:05:49 +00:00
|
|
|
arrayBuilder.flush(self.emptyFlushFunction);
|
2016-11-19 11:48:16 +00:00
|
|
|
arrayBuilder.submit();
|
|
|
|
|
|
|
|
[self assertMonotonicForInputSize:5 outputSize:5];
|
|
|
|
}
|
|
|
|
|
2016-11-19 00:27:08 +00:00
|
|
|
@end
|