Skip to content
Open
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
64 changes: 62 additions & 2 deletions DAWdleUnitTesting/UnitTesting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ namespace MemoryTests {
}

TEST(Strcmp, MultiByteCharacters) {
EXPECT_GT(strcmp("�stevan", "estevan"), 0);
EXPECT_GT(strcmp("éstevan", "estevan"), 0);
}

TEST(Strcmp, NullTerminatedCheck) {
Expand Down Expand Up @@ -811,4 +811,64 @@ namespace MemoryTests {
char buffer2[10] = "hello";
EXPECT_EQ(memcmp(buffer1, buffer2, 3), 0);
}
}
}

namespace ByteBuffer {

const StrA str = "dawdleDotcom"sa;

TEST(ByteBuff, Wrap) {
ByteBuf test;
U8 testValue = 'a';
U8* testdata = &testValue;
U32 dataSize = sizeof(testdata);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you want to take the size of your underlying data rather than the size of the pointer

test.wrap(testdata, dataSize);
EXPECT_EQ(test.bytes, 'a');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since test.bytes is a pointer this will check if the address of the byte buffer is equal to the numerical ascii code for a. You need some more custom code to validate that the contents of the buffer is correct

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's worth noting that the wrap method literally makes a wrapper around the pointer you pass in. In other words, test.bytes == testdata, so that's what you'd want to check if you're testing the wrap method.

}

TEST(ByteBuff, has_data_left) {
ByteBuf test;
U8 testValue = 'a';
U8* testdata = &testValue;
U32 dataSize = sizeof(testdata);
test.wrap(testdata, dataSize);
EXPECT_TRUE(test.has_data_left(dataSize));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While good for a start, I would add a little more logic to help ensure has_data_left works correctly. For example, wrap a larger buffer, test that it has the full amount left, read n bytes, then test that it has bufferSize - n bytes left and that it doesn't have bufferSize - n + 1 bytes left.

}

TEST(ByteBuff, skip) {
ByteBuf test;
U8 testValue = 'a';
U8* testdata = &testValue;
U32 dataSize = sizeof(testdata);
test.wrap(testdata, dataSize);
test.skip(1);
EXPECT_EQ(test.bytes, 'a');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entire sure what's being tested here. You could check that test.bytes is unmodified after the skip, or that it did indeed skip one byte, resulting in a zero length buffer.

}

TEST(ByteBuff, read_bytes) {
ByteBuf test;
U8 testValue = 'a';
U8* testdata = &testValue;
U32 dataSize = sizeof(testdata);
test.read_bytes(testdata, dataSize);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't read it into the same memory location the buffer is using in the first place. An empty implementation of read_bytes would pass that. Instead, read the bytes into a different block of memory.

EXPECT_EQ(test.bytes, 'a');
}

TEST(ByteBuff, write_bytes) {
ByteBuf test;
ByteBuf result;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason write_bytes returns a ByteBuf& is so that you can chain function calls together. For example,
buf.write_f32(...).write_bytes(...).write_bytes(...).write_stra("myString"sa);
I don't think it's particularly useful to assign the result to another variable, since all that does is make an extra copy.

U8 testValue = 'a';
U8* testdata = &testValue;
U32 dataSize = sizeof(testdata);
result = test.write_bytes(testdata, dataSize);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no need to make a copy of test here since write bytes operates on the original buffer

EXPECT_EQ(test.bytes, result);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not the right comparison to make. You'd want to ensure test.bytes contains the same data as the testValue you put inside it

}

TEST(ByteBuff, write_stra) {
ByteBuf test;
ByteBuf result;
result = test.write_stra(str);
EXPECT_EQ(test.bytes, result);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The setup is wrong, and this comparison makes little sense. If you want to test serialization of a string, you'll have to give the ByteBuf a buffer to serialize into (wrap a U8[1024], for example). To test write_stra, inspect the byte buffer to make sure that it contains the string length in 32 bit little endian followed by the string's content, exactly as long as the preceding length.

}

}