Working on unit tests for class world
This commit is contained in:
@@ -41,6 +41,10 @@ int64_t StreamBuffer::total_writes() const {
|
||||
return (write_cursor_ - buf_lo_) + pre_read_count_;
|
||||
}
|
||||
|
||||
int64_t StreamBuffer::fill() const {
|
||||
return write_cursor_ - read_cursor_;
|
||||
}
|
||||
|
||||
bool StreamBuffer::layout_is(int64_t a, int64_t b, int64_t c) {
|
||||
if (read_cursor_ - buf_lo_ != a) return false;
|
||||
if (write_cursor_ - read_cursor_ != b) return false;
|
||||
@@ -400,6 +404,14 @@ void StreamBuffer::copy_into(StreamBuffer *sb) {
|
||||
sb->write_bytes(read_cursor_, write_cursor_ - read_cursor_);
|
||||
}
|
||||
|
||||
bool StreamBuffer::contents_equal(const StreamBuffer *other) const {
|
||||
int64_t len = fill();
|
||||
if (len != other->fill()) {
|
||||
return false;
|
||||
}
|
||||
return memcmp(read_cursor_, other->read_cursor_, len) == 0;
|
||||
}
|
||||
|
||||
util::HashValue StreamBuffer::hash() const {
|
||||
uint64_t hash1 = 0x82A7912E7893AC87;
|
||||
uint64_t hash2 = 0x81D402740DE458F3;
|
||||
@@ -448,90 +460,90 @@ LuaDefine(unittests_streambuffer, "c") {
|
||||
StreamBuffer sb11(11, true);
|
||||
|
||||
// Check the initial state.
|
||||
assert(sb11.layout_is(0, 0, 11));
|
||||
LuaAssert(L, sb11.layout_is(0, 0, 11));
|
||||
|
||||
// Write a few bytes.
|
||||
write_ztbytes(&sb11, "abcdef");
|
||||
assert(sb11.layout_is(0, 6, 5));
|
||||
LuaAssert(L, sb11.layout_is(0, 6, 5));
|
||||
|
||||
// Try reading some bytes.
|
||||
assert(streq("abcd", sb11.read_bytes(4)));
|
||||
assert(sb11.layout_is(4, 2, 5));
|
||||
LuaAssert(L, streq("abcd", sb11.read_bytes(4)));
|
||||
LuaAssert(L, sb11.layout_is(4, 2, 5));
|
||||
|
||||
// Put back two bytes.
|
||||
sb11.unread_to(2);
|
||||
assert(sb11.layout_is(2, 4, 5));
|
||||
LuaAssert(L, sb11.layout_is(2, 4, 5));
|
||||
|
||||
// Read some more bytes.
|
||||
assert(streq("cdef", sb11.read_bytes(4)));
|
||||
assert(sb11.layout_is(6, 0, 5));
|
||||
LuaAssert(L, streq("cdef", sb11.read_bytes(4)));
|
||||
LuaAssert(L, sb11.layout_is(6, 0, 5));
|
||||
|
||||
// Reading bytes now should raise an EOF and should not alter layout
|
||||
try {
|
||||
sb11.read_bytes(1);
|
||||
assert(false && "This should have thrown an exception");
|
||||
LuaAssert(L, false && "This should have thrown an exception");
|
||||
} catch (StreamEof) {}
|
||||
assert(sb11.layout_is(6, 0, 5));
|
||||
LuaAssert(L, sb11.layout_is(6, 0, 5));
|
||||
|
||||
// Write some more bytes into the stream, forcing a shift-left
|
||||
write_ztbytes(&sb11, "ghijkl");
|
||||
assert(sb11.layout_is(0, 6, 5));
|
||||
LuaAssert(L, sb11.layout_is(0, 6, 5));
|
||||
|
||||
// Test buffer wrapping a little more.
|
||||
assert(streq("ghi", sb11.read_bytes(3)));
|
||||
assert(sb11.layout_is(3, 3, 5));
|
||||
LuaAssert(L, streq("ghi", sb11.read_bytes(3)));
|
||||
LuaAssert(L, sb11.layout_is(3, 3, 5));
|
||||
write_ztbytes(&sb11, "mnopqr");
|
||||
assert(sb11.layout_is(0, 9, 2));
|
||||
assert(streq("jklmnopqr", sb11.read_bytes(9)));
|
||||
assert(sb11.layout_is(9, 0, 2));
|
||||
LuaAssert(L, sb11.layout_is(0, 9, 2));
|
||||
LuaAssert(L, streq("jklmnopqr", sb11.read_bytes(9)));
|
||||
LuaAssert(L, sb11.layout_is(9, 0, 2));
|
||||
|
||||
// Test 1-byte integer ops.
|
||||
sb11.clear();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
sb11.write_int8(i);
|
||||
sb11.write_int8(i+100);
|
||||
assert(sb11.read_int8() == i);
|
||||
assert(sb11.read_int8() == i+100);
|
||||
LuaAssert(L, sb11.read_int8() == i);
|
||||
LuaAssert(L, sb11.read_int8() == i+100);
|
||||
}
|
||||
|
||||
// Test 2-byte integer ops.
|
||||
for (int i = 0; i < 10; i++) {
|
||||
sb11.write_int16(i);
|
||||
sb11.write_int16(i+10000);
|
||||
assert(sb11.read_int16() == i);
|
||||
assert(sb11.read_int16() == i+10000);
|
||||
LuaAssert(L, sb11.read_int16() == i);
|
||||
LuaAssert(L, sb11.read_int16() == i+10000);
|
||||
}
|
||||
|
||||
// Test 4-byte integer ops.
|
||||
for (int i = 0; i < 10; i++) {
|
||||
sb11.write_int32(i);
|
||||
sb11.write_int32(i+1000000);
|
||||
assert(sb11.read_int32() == i);
|
||||
assert(sb11.read_int32() == i+1000000);
|
||||
LuaAssert(L, sb11.read_int32() == i);
|
||||
LuaAssert(L, sb11.read_int32() == i+1000000);
|
||||
}
|
||||
|
||||
// Test 8-byte integer ops.
|
||||
for (int i = 0; i < 10; i++) {
|
||||
sb11.write_int64(i + 1000000);
|
||||
assert(sb11.read_int64() == i + 1000000);
|
||||
LuaAssert(L, sb11.read_int64() == i + 1000000);
|
||||
}
|
||||
|
||||
// Check the write count and read count accumulator ability.
|
||||
sb11.clear();
|
||||
assert(sb11.total_writes() == 0);
|
||||
assert(sb11.total_reads() == 0);
|
||||
LuaAssert(L, sb11.total_writes() == 0);
|
||||
LuaAssert(L, sb11.total_reads() == 0);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
assert(sb11.total_writes() == i * 8);
|
||||
LuaAssert(L, sb11.total_writes() == i * 8);
|
||||
sb11.write_int32(i);
|
||||
sb11.write_int32(i+1000000);
|
||||
assert(sb11.total_reads() == i * 8);
|
||||
assert(sb11.read_int32() == i);
|
||||
assert(sb11.read_int32() == i+1000000);
|
||||
LuaAssert(L, sb11.total_reads() == i * 8);
|
||||
LuaAssert(L, sb11.read_int32() == i);
|
||||
LuaAssert(L, sb11.read_int32() == i+1000000);
|
||||
}
|
||||
|
||||
// Try clearing the buffer.
|
||||
sb11.clear();
|
||||
assert(sb11.layout_is(0, 0, 11));
|
||||
LuaAssert(L, sb11.layout_is(0, 0, 11));
|
||||
|
||||
// Write a number then overwrite.
|
||||
for (int i = 0; i < 2; i++) {
|
||||
@@ -541,10 +553,10 @@ LuaDefine(unittests_streambuffer, "c") {
|
||||
sb11.write_int16(56);
|
||||
sb11.write_int16(78);
|
||||
sb11.overwrite_int16(wc, 90);
|
||||
assert(sb11.read_int16() == 12);
|
||||
assert(sb11.read_int16() == 90);
|
||||
assert(sb11.read_int16() == 56);
|
||||
assert(sb11.read_int16() == 78);
|
||||
LuaAssert(L, sb11.read_int16() == 12);
|
||||
LuaAssert(L, sb11.read_int16() == 90);
|
||||
LuaAssert(L, sb11.read_int16() == 56);
|
||||
LuaAssert(L, sb11.read_int16() == 78);
|
||||
}
|
||||
|
||||
// Try compact string encoding.
|
||||
@@ -552,10 +564,21 @@ LuaDefine(unittests_streambuffer, "c") {
|
||||
sb11.write_string("abc");
|
||||
sb11.write_string("");
|
||||
sb11.write_string("de");
|
||||
assert(sb11.layout_is(0, 8, 3));
|
||||
assert(sb11.read_string() == "abc");
|
||||
assert(sb11.read_string() == "");
|
||||
assert(sb11.read_string() == "de");
|
||||
LuaAssert(L, sb11.layout_is(0, 8, 3));
|
||||
LuaAssert(L, sb11.read_string() == "abc");
|
||||
LuaAssert(L, sb11.read_string() == "");
|
||||
LuaAssert(L, sb11.read_string() == "de");
|
||||
|
||||
// Make sure that contents_equal is not obviously broken.
|
||||
StreamBuffer eqsb1, eqsb2;
|
||||
eqsb1.write_int32(12);
|
||||
eqsb1.write_int32(34);
|
||||
eqsb2.write_int32(34);
|
||||
LuaAssert(L, !eqsb1.contents_equal(&eqsb2));
|
||||
eqsb1.read_int32();
|
||||
LuaAssert(L, eqsb1.contents_equal(&eqsb2));
|
||||
eqsb1.write_int32(34);
|
||||
LuaAssert(L, !eqsb1.contents_equal(&eqsb2));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user