diff options
author | Petteri Aimonen <jpa@npb.mail.kapsi.fi> | 2011-09-13 16:14:08 +0000 |
---|---|---|
committer | Petteri Aimonen <jpa@npb.mail.kapsi.fi> | 2011-09-13 16:14:08 +0000 |
commit | fcfc99f766f278564c062922a2857c21ac9af041 (patch) | |
tree | bad83fb95b48917d36ced09debc9e6e565ff7649 /tests/test_encode_callbacks.c | |
parent | d4abb63c052dc9d4c23ce72e498b4c7483f3b7cb (diff) |
Fixed a bunch of bugs related to callback fields.
Most importantly, callback fields in submessages were being overwritten with garbage, causing segfaults.
Additionally, converted PB_LTYPE_FIXED to PB_LTYPE_FIXED32 and PB_LTYPE_FIXED64. This makes the interface
a bit easier to use, and in addition runs faster.
git-svn-id: https://svn.kapsi.fi/jpa/nanopb@975 e3a754e5-d11d-0410-8d38-ebb782a927b9
Diffstat (limited to 'tests/test_encode_callbacks.c')
-rw-r--r-- | tests/test_encode_callbacks.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/tests/test_encode_callbacks.c b/tests/test_encode_callbacks.c index da2ee28e..f0a046de 100644 --- a/tests/test_encode_callbacks.c +++ b/tests/test_encode_callbacks.c @@ -15,6 +15,32 @@ bool encode_string(pb_ostream_t *stream, const pb_field_t *field, const void *ar return pb_encode_string(stream, (uint8_t*)str, strlen(str)); } +bool encode_int32(pb_ostream_t *stream, const pb_field_t *field, const void *arg) +{ + if (!pb_encode_tag_for_field(stream, field)) + return false; + + return pb_encode_varint(stream, 42); +} + +bool encode_fixed32(pb_ostream_t *stream, const pb_field_t *field, const void *arg) +{ + if (!pb_encode_tag_for_field(stream, field)) + return false; + + uint32_t value = 42; + return pb_enc_fixed32(stream, field, &value); +} + +bool encode_fixed64(pb_ostream_t *stream, const pb_field_t *field, const void *arg) +{ + if (!pb_encode_tag_for_field(stream, field)) + return false; + + uint64_t value = 42; + return pb_enc_fixed64(stream, field, &value); +} + int main() { uint8_t buffer[1024]; @@ -22,6 +48,15 @@ int main() TestMessage testmessage = {}; testmessage.stringvalue.funcs.encode = &encode_string; + testmessage.int32value.funcs.encode = &encode_int32; + testmessage.fixed32value.funcs.encode = &encode_fixed32; + testmessage.fixed64value.funcs.encode = &encode_fixed64; + + testmessage.has_submsg = true; + testmessage.submsg.stringvalue.funcs.encode = &encode_string; + testmessage.submsg.int32value.funcs.encode = &encode_int32; + testmessage.submsg.fixed32value.funcs.encode = &encode_fixed32; + testmessage.submsg.fixed64value.funcs.encode = &encode_fixed64; if (!pb_encode(&stream, TestMessage_fields, &testmessage)) return 1; |