Protocol Buffers - Google's data interchange format (grpc依赖)
https://developers.google.com/protocol-buffers/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
2.2 KiB
73 lines
2.2 KiB
16 years ago
|
|
||
16 years ago
|
#include <assert.h>
|
||
|
#include <stdio.h>
|
||
16 years ago
|
#include <stdlib.h>
|
||
16 years ago
|
#include "descriptor.c"
|
||
16 years ago
|
#include "upb_enum.c"
|
||
16 years ago
|
#include "upb_parse.c"
|
||
16 years ago
|
#include "upb_context.c"
|
||
|
#include "upb_msg.c"
|
||
|
#include "upb_table.c"
|
||
16 years ago
|
|
||
16 years ago
|
void test_get_v_uint64_t()
|
||
16 years ago
|
{
|
||
16 years ago
|
upb_status_t status;
|
||
16 years ago
|
|
||
16 years ago
|
uint8_t zero[] = {0x00};
|
||
16 years ago
|
void *zero_buf = zero;
|
||
16 years ago
|
uint64_t zero_val = 0;
|
||
16 years ago
|
status = get_v_uint64_t(&zero_buf, zero + sizeof(zero), &zero_val);
|
||
16 years ago
|
assert(status == UPB_STATUS_OK);
|
||
16 years ago
|
assert(zero_val == 0);
|
||
|
assert(zero_buf == zero + sizeof(zero));
|
||
|
|
||
16 years ago
|
uint8_t one[] = {0x01};
|
||
16 years ago
|
void *one_buf = one;
|
||
16 years ago
|
uint64_t one_val = 0;
|
||
16 years ago
|
status = get_v_uint64_t(&one_buf, one + sizeof(one), &one_val);
|
||
16 years ago
|
assert(status == UPB_STATUS_OK);
|
||
16 years ago
|
assert(one_val == 1);
|
||
16 years ago
|
assert(one_buf == one + sizeof(one));
|
||
16 years ago
|
|
||
16 years ago
|
uint8_t twobyte[] = {0xAC, 0x02};
|
||
16 years ago
|
void *twobyte_buf = twobyte;
|
||
16 years ago
|
uint64_t twobyte_val = 0;
|
||
16 years ago
|
status = get_v_uint64_t(&twobyte_buf, twobyte + sizeof(twobyte), &twobyte_val);
|
||
16 years ago
|
assert(status == UPB_STATUS_OK);
|
||
16 years ago
|
assert(twobyte_val == 300);
|
||
16 years ago
|
assert(twobyte_buf == twobyte + sizeof(twobyte));
|
||
16 years ago
|
|
||
16 years ago
|
uint8_t tenbyte[] = {0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x7F};
|
||
16 years ago
|
void *tenbyte_buf = tenbyte;
|
||
16 years ago
|
uint64_t tenbyte_val = 0;
|
||
16 years ago
|
status = get_v_uint64_t(&tenbyte_buf, tenbyte + sizeof(tenbyte), &tenbyte_val);
|
||
16 years ago
|
assert(status == UPB_STATUS_OK);
|
||
|
assert(tenbyte_val == 0x89101c305080c101);
|
||
16 years ago
|
assert(tenbyte_buf == tenbyte + sizeof(tenbyte));
|
||
16 years ago
|
|
||
|
uint8_t elevenbyte[] = {0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01};
|
||
16 years ago
|
void *elevenbyte_buf = elevenbyte;
|
||
16 years ago
|
uint64_t elevenbyte_val = 0;
|
||
16 years ago
|
status = get_v_uint64_t(&elevenbyte_buf, elevenbyte + sizeof(elevenbyte), &elevenbyte_val);
|
||
16 years ago
|
assert(status == UPB_ERROR_UNTERMINATED_VARINT);
|
||
16 years ago
|
status = get_v_uint64_t(&elevenbyte_buf, elevenbyte + sizeof(elevenbyte)-1, &elevenbyte_val);
|
||
16 years ago
|
/* Byte 10 is 0x80, so we know it's unterminated. */
|
||
|
assert(status == UPB_ERROR_UNTERMINATED_VARINT);
|
||
16 years ago
|
status = get_v_uint64_t(&elevenbyte_buf, elevenbyte + sizeof(elevenbyte)-2, &elevenbyte_val);
|
||
16 years ago
|
assert(status == UPB_STATUS_NEED_MORE_DATA);
|
||
16 years ago
|
}
|
||
|
|
||
16 years ago
|
void test_upb_context() {
|
||
|
struct upb_context c;
|
||
|
assert(upb_context_init(&c));
|
||
|
upb_context_free(&c);
|
||
|
}
|
||
|
|
||
16 years ago
|
int main()
|
||
|
{
|
||
16 years ago
|
test_get_v_uint64_t();
|
||
16 years ago
|
test_upb_context();
|
||
16 years ago
|
printf("All tests passed.\n");
|
||
|
return 0;
|
||
|
}
|