aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_encode_callbacks.c
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@npb.mail.kapsi.fi>2011-09-12 18:53:33 +0000
committerPetteri Aimonen <jpa@npb.mail.kapsi.fi>2011-09-12 18:53:33 +0000
commitd4abb63c052dc9d4c23ce72e498b4c7483f3b7cb (patch)
tree340d5abed388ef4abda42f6e4208948abfb9efb9 /tests/test_encode_callbacks.c
parent35814517ad9da0edad3905269ea41facfb120f3c (diff)
Tests for callback fields
git-svn-id: https://svn.kapsi.fi/jpa/nanopb@974 e3a754e5-d11d-0410-8d38-ebb782a927b9
Diffstat (limited to 'tests/test_encode_callbacks.c')
-rw-r--r--tests/test_encode_callbacks.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/tests/test_encode_callbacks.c b/tests/test_encode_callbacks.c
new file mode 100644
index 00000000..da2ee28e
--- /dev/null
+++ b/tests/test_encode_callbacks.c
@@ -0,0 +1,33 @@
+/* Encoding testcase for callback fields */
+
+#include <stdio.h>
+#include <string.h>
+#include <pb_encode.h>
+#include "callbacks.pb.h"
+
+bool encode_string(pb_ostream_t *stream, const pb_field_t *field, const void *arg)
+{
+ char *str = "Hello world!";
+
+ if (!pb_encode_tag_for_field(stream, field))
+ return false;
+
+ return pb_encode_string(stream, (uint8_t*)str, strlen(str));
+}
+
+int main()
+{
+ uint8_t buffer[1024];
+ pb_ostream_t stream = pb_ostream_from_buffer(buffer, 1024);
+ TestMessage testmessage = {};
+
+ testmessage.stringvalue.funcs.encode = &encode_string;
+
+ if (!pb_encode(&stream, TestMessage_fields, &testmessage))
+ return 1;
+
+ if (fwrite(buffer, stream.bytes_written, 1, stdout) != 1)
+ return 2;
+
+ return 0;
+}