From 0b128565c053629035324203e6dcb29726745485 Mon Sep 17 00:00:00 2001 From: David Lamparter Date: Mon, 27 Jun 2016 15:35:06 +0200 Subject: [PATCH] tests: duplicate stream tests in packed variant --- tests/capn-stream-test.cpp | 96 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/tests/capn-stream-test.cpp b/tests/capn-stream-test.cpp index f424648..ee55f7f 100644 --- a/tests/capn-stream-test.cpp +++ b/tests/capn-stream-test.cpp @@ -98,6 +98,24 @@ TEST(Stream, WriteEmptyStream) { capn_free(&ctx2); } +TEST(Stream, WriteEmptyStreamPacked) { + uint8_t buf[2048]; + + struct capn ctx1, ctx2; + capn_init_malloc(&ctx1); + struct capn_ptr root = capn_root(&ctx1); + ASSERT_EQ(CAPN_PTR_LIST, root.type); + EXPECT_EQ(-1, capn_write_mem(&ctx1, buf, 3, 1)); + EXPECT_EQ(4, capn_write_mem(&ctx1, buf, 2048, 1)); + ASSERT_EQ(0, capn_init_mem(&ctx2, buf, 2048, 1)); + EXPECT_EQ(1, ctx2.segnum); + EXPECT_EQ(8, ctx2.seglist->len); + EXPECT_EQ(0, ctx2.seglist->next); + + capn_free(&ctx1); + capn_free(&ctx2); +} + TEST(Stream, WriteOneSegment) { uint8_t buf[2048]; @@ -122,6 +140,30 @@ TEST(Stream, WriteOneSegment) { capn_free(&ctx2); } +TEST(Stream, WriteOneSegmentPacked) { + uint8_t buf[2048]; + + struct capn ctx1, ctx2; + capn_init_malloc(&ctx1); + + struct capn_ptr root = capn_root(&ctx1); + struct capn_ptr ptr = capn_new_struct(root.seg, 8, 0); + EXPECT_EQ(0, capn_setp(root, 0, ptr)); + EXPECT_EQ(0, capn_write64(ptr, 0, UINT64_C(0x1011121314151617))); + + EXPECT_EQ(-1, capn_write_mem(&ctx1, buf, 13, 1)); + EXPECT_EQ(14, capn_write_mem(&ctx1, buf, 2048, 1)); + ASSERT_EQ(0, capn_init_mem(&ctx2, buf, 2048, 1)); + EXPECT_EQ(1, ctx2.segnum); + + root = capn_root(&ctx2); + ptr = capn_getp(root, 0, 1); + EXPECT_EQ(UINT64_C(0x1011121314151617), capn_read64(ptr, 0)); + + capn_free(&ctx1); + capn_free(&ctx2); +} + static struct capn_segment *CreateSmallSegment(void *u, uint32_t id, int sz) { struct capn_segment *s = (struct capn_segment*) calloc(1, sizeof(*s)); s->data = (char*) calloc(1, sz); @@ -156,6 +198,33 @@ TEST(Stream, WriteTwoSegments) { capn_free(&ctx2); } +TEST(Stream, WriteTwoSegmentsPacked) { + struct capn ctx1, ctx2; + uint8_t buf[5*8]; + + capn_init_malloc(&ctx1); + ctx1.create = &CreateSmallSegment; + struct capn_ptr root = capn_root(&ctx1); + struct capn_ptr ptr1 = capn_new_struct(root.seg, 8, 0); + EXPECT_EQ(0, capn_setp(root, 0, ptr1)); + EXPECT_EQ(0, capn_write64(ptr1, 0, UINT64_C(0xfffefdfcfbfaf9f8))); + EXPECT_EQ(2, ctx1.segnum); + + /* 2 words: header + * 1 word: segment 1 + * 2 words: segment 2 + */ + EXPECT_EQ(20, capn_write_mem(&ctx1, buf, 5*8, 1)); + + ASSERT_EQ(0, capn_init_mem(&ctx2, buf, 2048, 1)); + root = capn_root(&ctx2); + ptr1 = capn_getp(root, 0, 1); + EXPECT_EQ(UINT64_C(0xfffefdfcfbfaf9f8), capn_read64(ptr1, 0)); + + capn_free(&ctx1); + capn_free(&ctx2); +} + TEST(Stream, WriteThreeSegments) { struct capn ctx1, ctx2; uint8_t buf[2048]; @@ -182,3 +251,30 @@ TEST(Stream, WriteThreeSegments) { capn_free(&ctx1); capn_free(&ctx2); } + +TEST(Stream, WriteThreeSegmentsPacked) { + struct capn ctx1, ctx2; + uint8_t buf[2048]; + + capn_init_malloc(&ctx1); + ctx1.create = &CreateSmallSegment; + struct capn_ptr root = capn_root(&ctx1); + struct capn_ptr ptr1 = capn_new_struct(root.seg, 0, 1); + EXPECT_EQ(0, capn_setp(root, 0, ptr1)); + struct capn_ptr ptr2 = capn_new_struct(ptr1.seg, 4, 0); + EXPECT_EQ(0, capn_setp(ptr1, 0, ptr2)); + EXPECT_EQ(0, capn_write32(ptr2, 0, 0x12345678)); + EXPECT_EQ(3, ctx1.segnum); + + EXPECT_EQ(-1, capn_write_mem(&ctx1, buf, 20, 1)); + EXPECT_EQ(21, capn_write_mem(&ctx1, buf, 2048, 1)); + + EXPECT_EQ(0, capn_init_mem(&ctx2, buf, 2048, 1)); + root = capn_root(&ctx2); + ptr1 = capn_getp(root, 0, 1); + ptr2 = capn_getp(ptr1, 0, 1); + EXPECT_EQ(0x12345678, capn_read32(ptr2, 0)); + + capn_free(&ctx1); + capn_free(&ctx2); +}