#include <stdio.h>#include <stddef.h>#include <stdint.h>#include <setjmp.h>#include <string.h>#include <stdbool.h>#include <stdarg.h>#include <cmocka.h> struct integers { //# decoder encoder short s; //#1 pretty=short signed int i; //#2 pretty=int signed long l; //#3 pretty=long signed int8_t i8; //#4 pretty=int8_t signed uint8_t u8; //#5 pretty=uint8_t int16_t i16; //#6 pretty=int16_t signed uint16_t u16; //#7 pretty=uint16_t int32_t i32; //#8 pretty=int32_t signed uint32_t u32; //#9 pretty=uint32_t int64_t i64; //#10 pretty=int64_t signed uint64_t u64; //#11 pretty=uint64_t bool b; //#127 pretty=bool}; #include "test_integers.pb.h" static voidset_integers(struct integers *is, int64_t i){ memset(is, 0, sizeof(*is)); is->s = i; is->i = i; is->l = i; is->i8 = i; is->u8 = i; is->i16 = i; is->u16 = i; is->i32 = i; is->u32 = i; is->i64 = i; is->u64 = i; is->b = !!i;} voidtest_integers_default_pretty(void **state){ struct integers is; char buf[128]; (void)state; set_integers(&is, 0); assert_non_null(pb_seprint_integers(&is, buf, buf+sizeof(buf), 0, 0)); assert_string_equal( buf, "{}" );} voidtest_integers_pretty(void **state){ struct integers is; char buf[256]; (void)state; set_integers(&is, 1); assert_non_null(pb_seprint_integers(&is, buf, buf+sizeof(buf), 0, 1)); assert_string_equal( buf, "{\n" " short = 1, // #1\n" " int = 1, // #2\n" " long = 1, // #3\n" " int8_t = 1, // #4\n" " uint8_t = 1, // #5\n" " int16_t = 1, // #6\n" " uint16_t = 1, // #7\n" " int32_t = 1, // #8\n" " uint32_t = 1, // #9\n" " int64_t = 1, // #10\n" " uint64_t = 1, // #11\n" " bool = 1, // #127\n" "}" );} voidtest_integers_max_unsigned_pretty(void **state){ struct integers is; char buf[256]; (void)state; memset(&is, 0, sizeof(is)); is.u8 = 0xff; is.u16 = 0xffff; is.u32 = 0xffffffff; is.u64 = 0xffffffffffffffff; assert_non_null(pb_seprint_integers(&is, buf, buf+sizeof(buf), 0, 1)); assert_string_equal( buf, "{\n" " uint8_t = 255, // #5\n" " uint16_t = 65535, // #7\n" " uint32_t = 4294967295, // #9\n" " uint64_t = 18446744073709551615, // #11\n" "}" );} voidtest_integers_min_signed_pretty(void **state){ struct integers is; char buf[256]; (void)state; memset(&is, 0, sizeof(is)); is.i8 = -0x80; is.i16 = -0x8000; is.i32 = -0x80000000; is.i64 = -0x8000000000000000; assert_non_null(pb_seprint_integers(&is, buf, buf+sizeof(buf), 0, 1)); assert_string_equal( buf, "{\n" " int8_t = -128, // #4\n" " int16_t = -32768, // #6\n" " int32_t = -2147483648, // #8\n" " int64_t = -9223372036854775808, // #10\n" "}" );} voidtest_integers_max_signed_pretty(void **state){ struct integers is; char buf[256]; (void)state; memset(&is, 0, sizeof(is)); is.i8 = 0x7f; is.i16 = 0x7fff; is.i32 = 0x7fffffff; is.i64 = 0x7fffffffffffffff; assert_non_null(pb_seprint_integers(&is, buf, buf+sizeof(buf), 0, 1)); assert_string_equal( buf, "{\n" " int8_t = 127, // #4\n" " int16_t = 32767, // #6\n" " int32_t = 2147483647, // #8\n" " int64_t = 9223372036854775807, // #10\n" "}" );} voidtest_integers_decode(void **state){ // s:-1, i:1, l:-2, i8:2, u8:3, i16:-3, u16:4, i32:-2147483648, u32:5, i64:-9223372036854775808, u64:6, b:true const uint8_t raw[] = { 0x08, 0x01, 0x10, 0x02, 0x18, 0x03, 0x20, 0x04, 0x28, 0x03, 0x30, 0x05, 0x38, 0x04, 0x40, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x48, 0x05, 0x50, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x58, 0x06, 0xf8, 0x07, 0x01, }; struct integers is; (void)state; assert_ptr_equal(pb_decode_integers(&is, raw, raw+sizeof(raw)), raw+sizeof(raw)); assert_true(is.s == -1); assert_true(is.i == 1); assert_true(is.l == -2); assert_true(is.i8 == 2); assert_true(is.u8 == 3); assert_true(is.i16 == -3); assert_true(is.u16 == 4); assert_true(is.i32 == -2147483648); assert_true(is.u32 == 5); assert_true(is.i64 == -9223372036854775807LL - 1LL); assert_true(is.u64 == 6); assert_true(is.b == true);} voidtest_integers_decode_packed(void **state){ // s:-1, i:1, l:-2, i8:2, u8:3, i16:-3, u16:4, i32:-2147483648, u32:5, i64:-9223372036854775808, u64:6 const uint8_t raw[] = { 0x0a, 0x01, 0x01, 0x12, 0x01, 0x02, 0x1a, 0x01, 0x03, 0x22, 0x01, 0x04, 0x2a, 0x01, 0x03, 0x32, 0x01, 0x05, 0x3a, 0x01, 0x04, 0x42, 0x05, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x4a, 0x01, 0x05, 0x52, 0x0a, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x5a, 0x01, 0x06, }; struct integers is; (void)state; assert_ptr_equal(pb_decode_integers(&is, raw, raw+sizeof(raw)), raw+sizeof(raw)); assert_true(is.s == -1); assert_true(is.i == 1); assert_true(is.l == -2); assert_true(is.i8 == 2); assert_true(is.u8 == 3); assert_true(is.i16 == -3); assert_true(is.u16 == 4); assert_true(is.i32 == -2147483648); assert_true(is.u32 == 5); assert_true(is.i64 == -9223372036854775807LL - 1LL); assert_true(is.u64 == 6);} voidtest_integers_encode(void **state){ // s:-32768, i:-2147483648, l:-2147483648, i8:-128, u8:255, i16:-32768, u16:65535, i32:-2147483648, u32:4294967295, i64:-9223372036854775808, u64:18446744073709551615, b:true const uint8_t raw[] = { 0x08, 0xff, 0xff, 0x03, 0x10, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x18, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x20, 0xff, 0x01, 0x28, 0xff, 0x01, 0x30, 0xff, 0xff, 0x03, 0x38, 0xff, 0xff, 0x03, 0x40, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x48, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x50, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x58, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0xf8, 0x07, 0x01, }; struct integers is; uint8_t buf[sizeof(raw)]; (void)state; is.s = -32768; is.i = -2147483648; is.l = -2147483648; // this should really depend on the platform is.i8 = -128; is.u8 = 255; is.i16 = -32768; is.u16 = 65535; is.i32 = -2147483648; is.u32 = 4294967295; is.i64 = -9223372036854775807LL - 1LL; is.u64 = 18446744073709551615ULL; is.b = true; assert_ptr_equal(pb_encode_integers(&is, buf, buf+sizeof(buf)), buf+sizeof(raw)); assert_memory_equal(raw, buf, sizeof(raw));} voidtest_integers_encode_2(void **state){ // s:32767, i:2147483647, l:2147483647, i8:127, i16:32767, i32:2147483647, i64:9223372036854775807 const uint8_t raw[] = { 0x08, 0xfe, 0xff, 0x03, 0x10, 0xfe, 0xff, 0xff, 0xff, 0x0f, 0x18, 0xfe, 0xff, 0xff, 0xff, 0x0f, 0x20, 0xfe, 0x01, 0x30, 0xfe, 0xff, 0x03, 0x40, 0xfe, 0xff, 0xff, 0xff, 0x0f, 0x50, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, }; struct integers is; uint8_t buf[sizeof(raw)]; (void)state; memset(&is, 0, sizeof(is)); is.s = 32767; is.i = 2147483647; is.l = 2147483647; // this should really depend on the platform is.i8 = 127; is.i16 = 32767; is.i32 = 2147483647; is.i64 = 9223372036854775807; assert_ptr_equal(pb_encode_integers(&is, buf, buf+sizeof(buf)), buf+sizeof(raw)); assert_memory_equal(raw, buf, sizeof(raw));}
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include <setjmp.h>
#include <string.h>
#include <stdbool.h>
#include <stdarg.h>
#include <cmocka.h>
struct integers { //# decoder encoder
short s; //#1 pretty=short signed
int i; //#2 pretty=int signed
long l; //#3 pretty=long signed
int8_t i8; //#4 pretty=int8_t signed
uint8_t u8; //#5 pretty=uint8_t
int16_t i16; //#6 pretty=int16_t signed
uint16_t u16; //#7 pretty=uint16_t
int32_t i32; //#8 pretty=int32_t signed
uint32_t u32; //#9 pretty=uint32_t
int64_t i64; //#10 pretty=int64_t signed
uint64_t u64; //#11 pretty=uint64_t
bool b; //#127 pretty=bool
};
#include "test_integers.pb.h"
static void
set_integers(struct integers *is, int64_t i)
{
memset(is, 0, sizeof(*is));
is->s = i;
is->i = i;
is->l = i;
is->i8 = i;
is->u8 = i;
is->i16 = i;
is->u16 = i;
is->i32 = i;
is->u32 = i;
is->i64 = i;
is->u64 = i;
is->b = !!i;
}
void
test_integers_default_pretty(void **state)
{
struct integers is;
char buf[128];
(void)state;
set_integers(&is, 0);
assert_non_null(pb_seprint_integers(&is, buf, buf+sizeof(buf), 0, 0));
assert_string_equal(
buf,
"{}"
);
}
void
test_integers_pretty(void **state)
{
struct integers is;
char buf[256];
(void)state;
set_integers(&is, 1);
assert_non_null(pb_seprint_integers(&is, buf, buf+sizeof(buf), 0, 1));
assert_string_equal(
buf,
"{\n"
" short = 1, // #1\n"
" int = 1, // #2\n"
" long = 1, // #3\n"
" int8_t = 1, // #4\n"
" uint8_t = 1, // #5\n"
" int16_t = 1, // #6\n"
" uint16_t = 1, // #7\n"
" int32_t = 1, // #8\n"
" uint32_t = 1, // #9\n"
" int64_t = 1, // #10\n"
" uint64_t = 1, // #11\n"
" bool = 1, // #127\n"
"}"
);
}
void
test_integers_max_unsigned_pretty(void **state)
{
struct integers is;
char buf[256];
(void)state;
memset(&is, 0, sizeof(is));
is.u8 = 0xff;
is.u16 = 0xffff;
is.u32 = 0xffffffff;
is.u64 = 0xffffffffffffffff;
assert_non_null(pb_seprint_integers(&is, buf, buf+sizeof(buf), 0, 1));
assert_string_equal(
buf,
"{\n"
" uint8_t = 255, // #5\n"
" uint16_t = 65535, // #7\n"
" uint32_t = 4294967295, // #9\n"
" uint64_t = 18446744073709551615, // #11\n"
"}"
);
}
void
test_integers_min_signed_pretty(void **state)
{
struct integers is;
char buf[256];
(void)state;
memset(&is, 0, sizeof(is));
is.i8 = -0x80;
is.i16 = -0x8000;
is.i32 = -0x80000000;
is.i64 = -0x8000000000000000;
assert_non_null(pb_seprint_integers(&is, buf, buf+sizeof(buf), 0, 1));
assert_string_equal(
buf,
"{\n"
" int8_t = -128, // #4\n"
" int16_t = -32768, // #6\n"
" int32_t = -2147483648, // #8\n"
" int64_t = -9223372036854775808, // #10\n"
"}"
);
}
void
test_integers_max_signed_pretty(void **state)
{
struct integers is;
char buf[256];
(void)state;
memset(&is, 0, sizeof(is));
is.i8 = 0x7f;
is.i16 = 0x7fff;
is.i32 = 0x7fffffff;
is.i64 = 0x7fffffffffffffff;
assert_non_null(pb_seprint_integers(&is, buf, buf+sizeof(buf), 0, 1));
assert_string_equal(
buf,
"{\n"
" int8_t = 127, // #4\n"
" int16_t = 32767, // #6\n"
" int32_t = 2147483647, // #8\n"
" int64_t = 9223372036854775807, // #10\n"
"}"
);
}
void
test_integers_decode(void **state)
{
// s:-1, i:1, l:-2, i8:2, u8:3, i16:-3, u16:4, i32:-2147483648, u32:5, i64:-9223372036854775808, u64:6, b:true
const uint8_t raw[] = {
0x08, 0x01, 0x10, 0x02, 0x18, 0x03, 0x20, 0x04, 0x28, 0x03, 0x30, 0x05, 0x38, 0x04, 0x40, 0xff,
0xff, 0xff, 0xff, 0x0f, 0x48, 0x05, 0x50, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x01, 0x58, 0x06, 0xf8, 0x07, 0x01,
};
struct integers is;
(void)state;
assert_ptr_equal(pb_decode_integers(&is, raw, raw+sizeof(raw)), raw+sizeof(raw));
assert_true(is.s == -1);
assert_true(is.i == 1);
assert_true(is.l == -2);
assert_true(is.i8 == 2);
assert_true(is.u8 == 3);
assert_true(is.i16 == -3);
assert_true(is.u16 == 4);
assert_true(is.i32 == -2147483648);
assert_true(is.u32 == 5);
assert_true(is.i64 == -9223372036854775807LL - 1LL);
assert_true(is.u64 == 6);
assert_true(is.b == true);
}
void
test_integers_decode_packed(void **state)
{
// s:-1, i:1, l:-2, i8:2, u8:3, i16:-3, u16:4, i32:-2147483648, u32:5, i64:-9223372036854775808, u64:6
const uint8_t raw[] = {
0x0a, 0x01, 0x01, 0x12, 0x01, 0x02, 0x1a, 0x01, 0x03, 0x22, 0x01, 0x04, 0x2a, 0x01, 0x03, 0x32,
0x01, 0x05, 0x3a, 0x01, 0x04, 0x42, 0x05, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x4a, 0x01, 0x05, 0x52,
0x0a, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x5a, 0x01, 0x06,
};
struct integers is;
(void)state;
assert_ptr_equal(pb_decode_integers(&is, raw, raw+sizeof(raw)), raw+sizeof(raw));
assert_true(is.s == -1);
assert_true(is.i == 1);
assert_true(is.l == -2);
assert_true(is.i8 == 2);
assert_true(is.u8 == 3);
assert_true(is.i16 == -3);
assert_true(is.u16 == 4);
assert_true(is.i32 == -2147483648);
assert_true(is.u32 == 5);
assert_true(is.i64 == -9223372036854775807LL - 1LL);
assert_true(is.u64 == 6);
}
void
test_integers_encode(void **state)
{
// s:-32768, i:-2147483648, l:-2147483648, i8:-128, u8:255, i16:-32768, u16:65535, i32:-2147483648, u32:4294967295, i64:-9223372036854775808, u64:18446744073709551615, b:true
const uint8_t raw[] = {
0x08, 0xff, 0xff, 0x03, 0x10, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x18, 0xff, 0xff, 0xff, 0xff, 0x0f,
0x20, 0xff, 0x01, 0x28, 0xff, 0x01, 0x30, 0xff, 0xff, 0x03, 0x38, 0xff, 0xff, 0x03, 0x40, 0xff,
0xff, 0xff, 0xff, 0x0f, 0x48, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x50, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x01, 0x58, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01,
0xf8, 0x07, 0x01,
};
struct integers is;
uint8_t buf[sizeof(raw)];
(void)state;
is.s = -32768;
is.i = -2147483648;
is.l = -2147483648; // this should really depend on the platform
is.i8 = -128;
is.u8 = 255;
is.i16 = -32768;
is.u16 = 65535;
is.i32 = -2147483648;
is.u32 = 4294967295;
is.i64 = -9223372036854775807LL - 1LL;
is.u64 = 18446744073709551615ULL;
is.b = true;
assert_ptr_equal(pb_encode_integers(&is, buf, buf+sizeof(buf)), buf+sizeof(raw));
assert_memory_equal(raw, buf, sizeof(raw));
}
void
test_integers_encode_2(void **state)
{
// s:32767, i:2147483647, l:2147483647, i8:127, i16:32767, i32:2147483647, i64:9223372036854775807
const uint8_t raw[] = {
0x08, 0xfe, 0xff, 0x03, 0x10, 0xfe, 0xff, 0xff, 0xff, 0x0f, 0x18, 0xfe, 0xff, 0xff, 0xff, 0x0f,
0x20, 0xfe, 0x01, 0x30, 0xfe, 0xff, 0x03, 0x40, 0xfe, 0xff, 0xff, 0xff, 0x0f, 0x50, 0xfe, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01,
};
struct integers is;
uint8_t buf[sizeof(raw)];
(void)state;
memset(&is, 0, sizeof(is));
is.s = 32767;
is.i = 2147483647;
is.l = 2147483647; // this should really depend on the platform
is.i8 = 127;
is.i16 = 32767;
is.i32 = 2147483647;
is.i64 = 9223372036854775807;
assert_ptr_equal(pb_encode_integers(&is, buf, buf+sizeof(buf)), buf+sizeof(raw));
assert_memory_equal(raw, buf, sizeof(raw));
}