5 #define COMMENT(x) printf("\n----" x "----\n");
10 fprintf(stderr, __FILE__ ":" STR2(__LINE__) " FAILED:" #x "\n"); \
13 printf("OK: " #x "\n"); \
16 #define S(x) pb_istream_from_buffer((uint8_t*)x, sizeof(x))
18 bool stream_callback(pb_istream_t *stream, uint8_t *buf, size_t count)
20 if (stream->state != NULL)
21 return false; /* Simulate error */
24 memset(buf, 'x', count);
33 uint8_t buffer1[] = "foobartest1234";
34 uint8_t buffer2[sizeof(buffer1)];
35 pb_istream_t stream = pb_istream_from_buffer(buffer1, sizeof(buffer1));
37 COMMENT("Test pb_read and pb_istream_t");
38 TEST(pb_read(&stream, buffer2, 6))
39 TEST(memcmp(buffer2, "foobar", 6) == 0)
40 TEST(stream.bytes_left == sizeof(buffer1) - 6)
41 TEST(pb_read(&stream, buffer2 + 6, stream.bytes_left))
42 TEST(memcmp(buffer1, buffer2, sizeof(buffer1)) == 0)
43 TEST(stream.bytes_left == 0)
44 TEST(!pb_read(&stream, buffer2, 1))
49 pb_istream_t stream = {&stream_callback, NULL, 20};
51 COMMENT("Test pb_read with custom callback");
52 TEST(pb_read(&stream, buffer, 5))
53 TEST(memcmp(buffer, "xxxxx", 5) == 0)
54 TEST(!pb_read(&stream, buffer, 50))
55 stream.state = (void*)1; /* Simulated error return from callback */
56 TEST(!pb_read(&stream, buffer, 5))
58 TEST(pb_read(&stream, buffer, 15))
66 COMMENT("Test pb_decode_varint32");
67 TEST((s = S("\x00"), pb_decode_varint32(&s, &u) && u == 0));
68 TEST((s = S("\x01"), pb_decode_varint32(&s, &u) && u == 1));
69 TEST((s = S("\xAC\x02"), pb_decode_varint32(&s, &u) && u == 300));
70 TEST((s = S("\xFF\xFF\xFF\xFF\x0F"), pb_decode_varint32(&s, &u) && u == UINT32_MAX));
71 TEST((s = S("\xFF\xFF\xFF\xFF\x0F"), pb_decode_varint32(&s, (uint32_t*)&i) && i == -1));
79 COMMENT("Test pb_decode_varint64");
80 TEST((s = S("\x00"), pb_decode_varint64(&s, &u) && u == 0));
81 TEST((s = S("\x01"), pb_decode_varint64(&s, &u) && u == 1));
82 TEST((s = S("\xAC\x02"), pb_decode_varint64(&s, &u) && u == 300));
83 TEST((s = S("\xFF\xFF\xFF\xFF\x0F"), pb_decode_varint64(&s, &u) && u == UINT32_MAX));
84 TEST((s = S("\xFF\xFF\xFF\xFF\x0F"), pb_decode_varint64(&s, (uint64_t*)&i) && i == UINT32_MAX));
85 TEST((s = S("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"),
86 pb_decode_varint64(&s, (uint64_t*)&i) && i == -1));
87 TEST((s = S("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"),
88 pb_decode_varint64(&s, &u) && u == UINT64_MAX));
93 COMMENT("Test pb_skip_varint");
94 TEST((s = S("\x00""foobar"), pb_skip_varint(&s) && s.bytes_left == 7))
95 TEST((s = S("\xAC\x02""foobar"), pb_skip_varint(&s) && s.bytes_left == 7))
96 TEST((s = S("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01""foobar"),
97 pb_skip_varint(&s) && s.bytes_left == 7))
102 COMMENT("Test pb_skip_string")
103 TEST((s = S("\x00""foobar"), pb_skip_string(&s) && s.bytes_left == 7))
104 TEST((s = S("\x04""testfoobar"), pb_skip_string(&s) && s.bytes_left == 7))
108 pb_istream_t s = S("\x01\xFF\xFF\x03");
109 pb_field_t f = {1, PB_LTYPE_VARINT, 0, 0, 4, 0, 0};
111 COMMENT("Test pb_dec_varint using uint32_t")
112 TEST(pb_dec_varint(&s, &f, &d) && d == 1)
114 /* Verify that no more than data_size is written. */
117 TEST(pb_dec_varint(&s, &f, &d) && d == 0xFF)
122 pb_field_t f = {1, PB_LTYPE_SVARINT, 0, 0, 4, 0, 0};
125 COMMENT("Test pb_dec_svarint using int32_t")
126 TEST((s = S("\x01"), pb_dec_svarint(&s, &f, &d) && d == -1))
127 TEST((s = S("\x02"), pb_dec_svarint(&s, &f, &d) && d == 1))
128 TEST((s = S("\xfe\xff\xff\xff\x0f"), pb_dec_svarint(&s, &f, &d) && d == INT32_MAX))
129 TEST((s = S("\xff\xff\xff\xff\x0f"), pb_dec_svarint(&s, &f, &d) && d == INT32_MIN))
133 fprintf(stdout, "\n\nSome tests FAILED!\n");