mirror of
https://github.com/google/pebble.git
synced 2025-08-24 11:17:24 -04:00
Import of the watch repository from Pebble
This commit is contained in:
commit
3b92768480
10334 changed files with 2564465 additions and 0 deletions
21
third_party/nanopb/tests/map/SConscript
vendored
Normal file
21
third_party/nanopb/tests/map/SConscript
vendored
Normal file
|
@ -0,0 +1,21 @@
|
|||
# Example / test for handling 'map' type using the backwards compatibility
|
||||
# in protobuf specification:
|
||||
# https://developers.google.com/protocol-buffers/docs/proto3#maps
|
||||
|
||||
Import('env')
|
||||
|
||||
env.NanopbProto(['map', 'map.options'])
|
||||
|
||||
enc = env.Program(['encode_map.c',
|
||||
'map.pb.c',
|
||||
'$COMMON/pb_encode.o',
|
||||
'$COMMON/pb_common.o'])
|
||||
|
||||
dec = env.Program(['decode_map.c',
|
||||
'map.pb.c',
|
||||
'$COMMON/pb_decode.o',
|
||||
'$COMMON/pb_common.o'])
|
||||
|
||||
env.RunTest("message.pb", enc)
|
||||
env.RunTest("message.txt", [dec, 'message.pb'])
|
||||
|
60
third_party/nanopb/tests/map/decode_map.c
vendored
Normal file
60
third_party/nanopb/tests/map/decode_map.c
vendored
Normal file
|
@ -0,0 +1,60 @@
|
|||
/* Decode a message using map field */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <pb_decode.h>
|
||||
#include "map.pb.h"
|
||||
#include "test_helpers.h"
|
||||
#include "unittests.h"
|
||||
|
||||
/* Helper function to find an entry in the list. Not as efficient as a real
|
||||
* hashmap or similar would be, but suitable for small arrays. */
|
||||
MyMessage_NumbersEntry *find_entry(MyMessage *msg, const char *key)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < msg->numbers_count; i++)
|
||||
{
|
||||
if (strcmp(msg->numbers[i].key, key) == 0)
|
||||
{
|
||||
return &msg->numbers[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
uint8_t buffer[MyMessage_size];
|
||||
size_t count;
|
||||
|
||||
SET_BINARY_MODE(stdin);
|
||||
count = fread(buffer, 1, sizeof(buffer), stdin);
|
||||
|
||||
if (!feof(stdin))
|
||||
{
|
||||
printf("Message does not fit in buffer\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
{
|
||||
int status = 0;
|
||||
MyMessage msg = MyMessage_init_zero;
|
||||
MyMessage_NumbersEntry *e;
|
||||
pb_istream_t stream = pb_istream_from_buffer(buffer, count);
|
||||
|
||||
if (!pb_decode(&stream, MyMessage_fields, &msg))
|
||||
{
|
||||
fprintf(stderr, "Decoding failed\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
TEST((e = find_entry(&msg, "one")) && e->value == 1);
|
||||
TEST((e = find_entry(&msg, "two")) && e->value == 2);
|
||||
TEST((e = find_entry(&msg, "seven")) && e->value == 7);
|
||||
TEST(!find_entry(&msg, "zero"));
|
||||
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
37
third_party/nanopb/tests/map/encode_map.c
vendored
Normal file
37
third_party/nanopb/tests/map/encode_map.c
vendored
Normal file
|
@ -0,0 +1,37 @@
|
|||
/* Encode a message using map field */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <pb_encode.h>
|
||||
#include "map.pb.h"
|
||||
#include "test_helpers.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
uint8_t buffer[MyMessage_size];
|
||||
MyMessage msg = MyMessage_init_zero;
|
||||
pb_ostream_t stream;
|
||||
|
||||
/* Fill in the map entries */
|
||||
msg.numbers_count = 3;
|
||||
strncpy(msg.numbers[0].key, "one", sizeof(msg.numbers[0].key));
|
||||
strncpy(msg.numbers[1].key, "two", sizeof(msg.numbers[1].key));
|
||||
strncpy(msg.numbers[2].key, "seven", sizeof(msg.numbers[2].key));
|
||||
msg.numbers[0].value = 1;
|
||||
msg.numbers[1].value = 2;
|
||||
msg.numbers[2].value = 7;
|
||||
|
||||
stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
|
||||
|
||||
if (pb_encode(&stream, MyMessage_fields, &msg))
|
||||
{
|
||||
SET_BINARY_MODE(stdout);
|
||||
fwrite(buffer, 1, stream.bytes_written, stdout);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream));
|
||||
return 1;
|
||||
}
|
||||
}
|
2
third_party/nanopb/tests/map/map.options
vendored
Normal file
2
third_party/nanopb/tests/map/map.options
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
MyMessage.numbers max_count:10
|
||||
MyMessage.NumbersEntry.key max_size:16
|
6
third_party/nanopb/tests/map/map.proto
vendored
Normal file
6
third_party/nanopb/tests/map/map.proto
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
syntax = "proto3";
|
||||
|
||||
message MyMessage {
|
||||
map<string,uint32> numbers = 1;
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue