Commit 5f604f17 authored by Greg Williams's avatar Greg Williams
Browse files

Updated to use new version of byte_array

parent bcc8ea29
Loading
Loading
Loading
Loading
+35 −27
Original line number Diff line number Diff line
@@ -5,6 +5,9 @@
#include <stdarg.h>
#include <sys/param.h>

static ByteBuffer* append_formatted_cstring_va_list(ByteBuffer* buffer,
    const char* format, va_list args);

ByteArray ByteArray_Create(void* data, size_t len)
{
    return (ByteArray) {
@@ -78,26 +81,6 @@ ByteBuffer ByteBuffer_CreateAndAppendCString(void* data, size_t max_len, const c
    return buf;
}

ByteBuffer ByteBuffer_CreateAndAppendFormattedCString(void* data, size_t max_len, const char * format, ...)
{
    ByteBuffer buf = ByteBuffer_Create(data, max_len, 0);

    va_list args;
    va_start(args,format);

    uint8_t tmp[256];
    ByteBuffer tmpBuf = ByteBuffer_Create(tmp, sizeof(tmp), 0);

    int formattedSize = vsnprintf((void*)tmpBuf.array.data, tmpBuf.array.len, format, args);
    assert(formattedSize >= 0);
    tmpBuf.bytesUsed = (tmpBuf.array.len <= (size_t)formattedSize) ? tmpBuf.array.len : formattedSize;

    va_end(args);

    ByteBuffer_AppendBuffer(&buf, tmpBuf);
    return buf;
}

long ByteBuffer_BytesRemaining(const ByteBuffer buffer)
{
    assert(buffer.array.data != NULL);
@@ -177,24 +160,49 @@ ByteBuffer* ByteBuffer_AppendCString(ByteBuffer* buffer, const char* str)
    return buffer;
}


static ByteBuffer* append_formatted_cstring_va_list(ByteBuffer* buffer, const char* format, va_list args)
{
    char* start = (char*)&buffer->array.data[buffer->bytesUsed];
    long startLen = (long)buffer->bytesUsed;
    long maxLen = buffer->array.len;
    long remainingLen = ByteBuffer_BytesRemaining(*buffer);
    long extraLen = vsnprintf(start, remainingLen, format, args);
    if (startLen + extraLen >= maxLen) {
        return NULL;
    }
    buffer->bytesUsed += extraLen;
    return buffer;
}

ByteBuffer* ByteBuffer_AppendFormattedCString(ByteBuffer* buffer, const char * format, ...)
{
    assert(buffer != NULL);
    assert(buffer->array.data != NULL);

    bool failed = false;
    va_list args;
    va_start(args, format);

    uint8_t tmp[256];
    ByteBuffer tmpBuf = ByteBuffer_Create(tmp, sizeof(tmp), 0);
    failed = (append_formatted_cstring_va_list(buffer, format, args) == NULL);

    va_end(args);

    return failed ? NULL : buffer;
}

    int formattedSize = vsnprintf((void*)tmpBuf.array.data, tmpBuf.array.len, format, args);
    assert(formattedSize >= 0);
    tmpBuf.bytesUsed = (tmpBuf.array.len <= (size_t)formattedSize) ? tmpBuf.array.len : formattedSize;
ByteBuffer ByteBuffer_CreateAndAppendFormattedCString(void* data, size_t max_len, const char * format, ...)
{
    ByteBuffer buf = ByteBuffer_Create(data, max_len, 0);

    va_list args;
    va_start(args, format);
    if (append_formatted_cstring_va_list(&buf, format, args) == NULL) {
        memset(&buf, 0, sizeof(ByteBuffer));
    }
    va_end(args);

    return ByteBuffer_AppendBuffer(buffer, tmpBuf);
    return buf;
}

ByteBuffer* ByteBuffer_AppendDummyData(ByteBuffer* buffer, size_t len)
+59 −10
Original line number Diff line number Diff line
@@ -164,8 +164,8 @@ void test_ByteBuffer_Consume(void)
    TEST_ASSERT_EQUAL(totalConsumed, buffer.bytesUsed);

    lenToConsume = 2;
    totalConsumed += 2;
    ByteArray consumed2 = ByteBuffer_Consume(&buffer, lenToConsume+1); // request more than available
    totalConsumed += lenToConsume;
    ByteArray consumed2 = ByteBuffer_Consume(&buffer, lenToConsume);
    TEST_ASSERT_EQUAL_PTR(&array.data[3], consumed2.data);
    TEST_ASSERT_EQUAL(lenToConsume, consumed2.len);
    TEST_ASSERT_EQUAL_HEX8_ARRAY(&data[3], consumed2.data, lenToConsume);
@@ -189,13 +189,13 @@ void test_ByteBuffer_Append_should_append_bytes_to_the_buffer(void)
    uint8_t appendData[len];

    appendData[0] = 0xFCu;
    TEST_ASSERT_TRUE(ByteBuffer_Append(&buffer, appendData, 1));
    TEST_ASSERT_NOT_NULL(ByteBuffer_Append(&buffer, appendData, 1));
    TEST_ASSERT_EQUAL(1, buffer.bytesUsed);
    TEST_ASSERT_EQUAL_HEX8_ARRAY(appendData, buffer.array.data, 1);

    appendData[1] = 0xABu;
    appendData[2] = 0x8Cu;
    TEST_ASSERT_TRUE(ByteBuffer_Append(&buffer, &appendData[1], 2));
    TEST_ASSERT_NOT_NULL(ByteBuffer_Append(&buffer, &appendData[1], 2));
    TEST_ASSERT_EQUAL(3, buffer.bytesUsed);
    TEST_ASSERT_EQUAL_HEX8_ARRAY(appendData, buffer.array.data, 3);

@@ -216,13 +216,13 @@ void test_ByteBuffer_AppendArray_should_append_an_array_to_the_buffer(void)
    ByteArray appendArray0 = (ByteArray) {
        .data = appendData, .len = 1
    };
    TEST_ASSERT_TRUE(ByteBuffer_AppendArray(&buffer, appendArray0));
    TEST_ASSERT_NOT_NULL(ByteBuffer_AppendArray(&buffer, appendArray0));
    TEST_ASSERT_EQUAL(1, buffer.bytesUsed);
    TEST_ASSERT_EQUAL_HEX8_ARRAY(appendData, buffer.array.data, 1);

    appendData[1] = 0xABu;
    appendData[2] = 0x8Cu;
    TEST_ASSERT_TRUE(ByteBuffer_Append(&buffer, &appendData[1], 2));
    TEST_ASSERT_NOT_NULL(ByteBuffer_Append(&buffer, &appendData[1], 2));
    TEST_ASSERT_EQUAL(3, buffer.bytesUsed);
    TEST_ASSERT_EQUAL_HEX8_ARRAY(appendData, buffer.array.data, 3);

@@ -238,11 +238,11 @@ void test_ByteBuffer_AppendCString_should_append_a_C_string(void)
    size_t len = sizeof(data);
    ByteBuffer buffer = ByteBuffer_Create(data, len, 0);

    TEST_ASSERT_TRUE(ByteBuffer_AppendCString(&buffer, "Co"));
    TEST_ASSERT_NOT_NULL(ByteBuffer_AppendCString(&buffer, "Co"));
    TEST_ASSERT_EQUAL(2, buffer.bytesUsed);
    TEST_ASSERT_EQUAL_HEX8_ARRAY("Co", buffer.array.data, 2);

    TEST_ASSERT_TRUE(ByteBuffer_AppendCString(&buffer, "d"));
    TEST_ASSERT_NOT_NULL(ByteBuffer_AppendCString(&buffer, "d"));
    TEST_ASSERT_EQUAL(3, buffer.bytesUsed);
    TEST_ASSERT_EQUAL_HEX8_ARRAY("Cod", buffer.array.data, 3);

@@ -252,6 +252,55 @@ void test_ByteBuffer_AppendCString_should_append_a_C_string(void)
    TEST_ASSERT_EQUAL_HEX8_ARRAY("Cod", buffer.array.data, 3);
}

void test_ByteBuffer_AppendFormattedCString_should_append_a_generated_C_string_using_args(void)
{
    uint8_t data[42];
    size_t len = sizeof(data);
    ByteBuffer buffer = ByteBuffer_Create(data, len, 0);

    TEST_ASSERT_NOT_NULL(ByteBuffer_AppendFormattedCString(&buffer, "Stuff: value=%d desc='%s'", 12, "foo"));
    const char* expected0 = "Stuff: value=12 desc='foo'";
    TEST_ASSERT_EQUAL_STRING(expected0, buffer.array.data);
    TEST_ASSERT_EQUAL(strlen(expected0), buffer.bytesUsed);

    TEST_ASSERT_NOT_NULL(ByteBuffer_AppendFormattedCString(&buffer, "; stuff=0x%04X", 0x34));
    const char* expected1 = "Stuff: value=12 desc='foo'; stuff=0x0034";
    TEST_ASSERT_EQUAL(strlen(expected1), buffer.bytesUsed);
    TEST_ASSERT_EQUAL_STRING(expected1, buffer.array.data);

    // This is too long to append, so NULL should be returned along with bytesUsed being unmodified
    TEST_ASSERT_NULL(ByteBuffer_AppendFormattedCString(&buffer, " maximum=%.6f", 0.123456789f));
    TEST_ASSERT_EQUAL(strlen(expected1), buffer.bytesUsed);
}

void test_ByteBuffer_CreateAndAppendFormattedCString_should_append_a_generated_C_string_using_args(void)
{
    uint8_t data[1024];
    size_t len = sizeof(data);
    memset(data, 0, len);
    ByteBuffer buffer = 
        ByteBuffer_CreateAndAppendFormattedCString(data, sizeof(data),
            "Stuff: value=%d desc='%s'", 12, "foo");
    TEST_ASSERT_EQUAL_PTR(data, buffer.array.data);
    TEST_ASSERT_EQUAL(sizeof(data), buffer.array.len);
    const char* expected = "Stuff: value=12 desc='foo'";
    TEST_ASSERT_EQUAL_STRING(expected, buffer.array.data);
    TEST_ASSERT_EQUAL_INT(strlen(expected), buffer.bytesUsed);
}

void test_ByteBuffer_CreateAndAppendFormattedCString_should_return_NULL_if_formatted_string_could_not_fit_into_buffer(void)
{
    uint8_t data[39];
    size_t len = sizeof(data);
    memset(data, 0, len);
    ByteBuffer buffer = 
        ByteBuffer_CreateAndAppendFormattedCString(data, sizeof(data),
            "Stuff: value=%d desc='%s' fizzle=%0.4f", 12, "foo", 1.23456);
    TEST_ASSERT_EQUAL(0, buffer.bytesUsed);
    TEST_ASSERT_EQUAL(0, buffer.array.len);
    TEST_ASSERT_NULL(buffer.array.data);
}

void test_ByteBuffer_AppendDummyData_should_append_dummy_data_to_buffer(void)
{
    uint8_t data[] = {5, 5, 5};
@@ -261,11 +310,11 @@ void test_ByteBuffer_AppendDummyData_should_append_dummy_data_to_buffer(void)

    TEST_ASSERT_EQUAL(0, buffer.bytesUsed);

    TEST_ASSERT_TRUE(ByteBuffer_AppendDummyData(&buffer, 2));
    TEST_ASSERT_NOT_NULL(ByteBuffer_AppendDummyData(&buffer, 2));
    TEST_ASSERT_EQUAL(2, buffer.bytesUsed);
    TEST_ASSERT_EQUAL_HEX8_ARRAY(expectedData, buffer.array.data, 2);

    TEST_ASSERT_TRUE(ByteBuffer_AppendDummyData(&buffer, 1));
    TEST_ASSERT_NOT_NULL(ByteBuffer_AppendDummyData(&buffer, 1));
    TEST_ASSERT_EQUAL(3, buffer.bytesUsed);
    TEST_ASSERT_EQUAL_HEX8_ARRAY(expectedData, buffer.array.data, 3);

+0 −1
Original line number Diff line number Diff line
@@ -60,7 +60,6 @@ void test_KineticClient_flush_should_get_success_if_no_writes_are_in_progress(vo
void test_KineticClient_flush_should_expose_memory_error_from_CreateOperation(void)
{
    KineticSessionHandle DummyHandle = 17;
    KineticOperation operation;

    KineticController_CreateOperation_ExpectAndReturn(DummyHandle, NULL);
    
+0 −1
Original line number Diff line number Diff line
@@ -88,7 +88,6 @@ void test_KineticClient_GetNext_should_expose_memory_errors(void)
        .key = KeyBuffer,
        .value = ValueBuffer,
    };
    KineticOperation operation;

    KineticController_CreateOperation_ExpectAndReturn(DummyHandle, NULL);
    KineticStatus status = KineticClient_GetNext(DummyHandle, &entry, NULL);