parent
7f5fe52dfa
commit
2a85bef825
19 changed files with 509 additions and 409 deletions
@ -1,80 +0,0 @@ |
|||||||
|
|
||||||
-- Require "pb" first to ensure that the transitive require of "upb" is |
|
||||||
-- handled properly by the "pb" module. |
|
||||||
local pb = require "upb.pb" |
|
||||||
local upb = require "upb" |
|
||||||
local lunit = require "lunit" |
|
||||||
|
|
||||||
if _VERSION >= 'Lua 5.2' then |
|
||||||
_ENV = lunit.module("testupb_pb", "seeall") |
|
||||||
else |
|
||||||
module("testupb_pb", lunit.testcase, package.seeall) |
|
||||||
end |
|
||||||
|
|
||||||
local symtab = upb.SymbolTable{ |
|
||||||
upb.MessageDef{full_name = "TestMessage", fields = { |
|
||||||
upb.FieldDef{name = "i32", number = 1, type = upb.TYPE_INT32}, |
|
||||||
upb.FieldDef{name = "u32", number = 2, type = upb.TYPE_UINT32}, |
|
||||||
upb.FieldDef{name = "i64", number = 3, type = upb.TYPE_INT64}, |
|
||||||
upb.FieldDef{name = "u64", number = 4, type = upb.TYPE_UINT64}, |
|
||||||
upb.FieldDef{name = "dbl", number = 5, type = upb.TYPE_DOUBLE}, |
|
||||||
upb.FieldDef{name = "flt", number = 6, type = upb.TYPE_FLOAT}, |
|
||||||
upb.FieldDef{name = "bool", number = 7, type = upb.TYPE_BOOL}, |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
local factory = upb.MessageFactory(symtab); |
|
||||||
local TestMessage = factory:get_message_class("TestMessage") |
|
||||||
|
|
||||||
function test_parse_primitive() |
|
||||||
local binary_pb = |
|
||||||
"\008\128\128\128\128\002\016\128\128\128\128\004\024\128\128" |
|
||||||
.. "\128\128\128\128\128\002\032\128\128\128\128\128\128\128\001\041\000" |
|
||||||
.. "\000\000\000\000\000\248\063\053\000\000\096\064\056\001" |
|
||||||
local msg = TestMessage() |
|
||||||
pb.decode(msg, binary_pb) |
|
||||||
assert_equal(536870912, msg.i32) |
|
||||||
assert_equal(1073741824, msg.u32) |
|
||||||
assert_equal(1125899906842624, msg.i64) |
|
||||||
assert_equal(562949953421312, msg.u64) |
|
||||||
assert_equal(1.5, msg.dbl) |
|
||||||
assert_equal(3.5, msg.flt) |
|
||||||
assert_equal(true, msg.bool) |
|
||||||
|
|
||||||
local encoded = pb.encode(msg) |
|
||||||
local msg2 = TestMessage() |
|
||||||
pb.decode(msg2, encoded) |
|
||||||
assert_equal(536870912, msg.i32) |
|
||||||
assert_equal(1073741824, msg.u32) |
|
||||||
assert_equal(1125899906842624, msg.i64) |
|
||||||
assert_equal(562949953421312, msg.u64) |
|
||||||
assert_equal(1.5, msg.dbl) |
|
||||||
assert_equal(3.5, msg.flt) |
|
||||||
assert_equal(true, msg.bool) |
|
||||||
end |
|
||||||
|
|
||||||
function test_parse_string() |
|
||||||
local symtab = upb.SymbolTable{ |
|
||||||
upb.MessageDef{full_name = "TestMessage", fields = { |
|
||||||
upb.FieldDef{name = "str", number = 1, type = upb.TYPE_STRING}, |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
local factory = upb.MessageFactory(symtab); |
|
||||||
local TestMessage = factory:get_message_class("TestMessage") |
|
||||||
|
|
||||||
local binary_pb = "\010\005Hello" |
|
||||||
msg = TestMessage() |
|
||||||
pb.decode(msg, binary_pb) |
|
||||||
-- TODO(haberman): re-enable when this stuff works better. |
|
||||||
-- assert_equal("Hello", msg.str) |
|
||||||
end |
|
||||||
|
|
||||||
|
|
||||||
local stats = lunit.main() |
|
||||||
|
|
||||||
if stats.failed > 0 or stats.errors > 0 then |
|
||||||
error("One or more errors in test suite") |
|
||||||
end |
|
@ -1,89 +0,0 @@ |
|||||||
/*
|
|
||||||
** Functions for use by generated code. These are not public and users must |
|
||||||
** not call them directly. |
|
||||||
*/ |
|
||||||
|
|
||||||
#ifndef UPB_GENERATED_UTIL_H_ |
|
||||||
#define UPB_GENERATED_UTIL_H_ |
|
||||||
|
|
||||||
#include <stdint.h> |
|
||||||
#include "upb/msg.h" |
|
||||||
|
|
||||||
#include "upb/port_def.inc" |
|
||||||
|
|
||||||
#define PTR_AT(msg, ofs, type) (type*)((const char*)msg + ofs) |
|
||||||
|
|
||||||
UPB_INLINE const void *_upb_array_accessor(const void *msg, size_t ofs, |
|
||||||
size_t *size) { |
|
||||||
const upb_array *arr = *PTR_AT(msg, ofs, const upb_array*); |
|
||||||
if (arr) { |
|
||||||
if (size) *size = arr->len; |
|
||||||
return _upb_array_constptr(arr); |
|
||||||
} else { |
|
||||||
if (size) *size = 0; |
|
||||||
return NULL; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
UPB_INLINE void *_upb_array_mutable_accessor(void *msg, size_t ofs, |
|
||||||
size_t *size) { |
|
||||||
upb_array *arr = *PTR_AT(msg, ofs, upb_array*); |
|
||||||
if (arr) { |
|
||||||
if (size) *size = arr->len; |
|
||||||
return _upb_array_ptr(arr); |
|
||||||
} else { |
|
||||||
if (size) *size = 0; |
|
||||||
return NULL; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
UPB_INLINE void *_upb_array_resize_accessor(void *msg, size_t ofs, size_t size, |
|
||||||
upb_fieldtype_t type, |
|
||||||
upb_arena *arena) { |
|
||||||
upb_array **arr_ptr = PTR_AT(msg, ofs, upb_array*); |
|
||||||
upb_array *arr = *arr_ptr; |
|
||||||
if (!arr || arr->size < size) { |
|
||||||
return _upb_array_resize_fallback(arr_ptr, size, type, arena); |
|
||||||
} |
|
||||||
arr->len = size; |
|
||||||
return _upb_array_ptr(arr); |
|
||||||
} |
|
||||||
|
|
||||||
UPB_INLINE bool _upb_array_append_accessor(void *msg, size_t ofs, |
|
||||||
size_t elem_size, |
|
||||||
upb_fieldtype_t type, |
|
||||||
const void *value, |
|
||||||
upb_arena *arena) { |
|
||||||
upb_array **arr_ptr = PTR_AT(msg, ofs, upb_array*); |
|
||||||
upb_array *arr = *arr_ptr; |
|
||||||
void* ptr; |
|
||||||
if (!arr || arr->len == arr->size) { |
|
||||||
return _upb_array_append_fallback(arr_ptr, value, type, arena); |
|
||||||
} |
|
||||||
ptr = _upb_array_ptr(arr); |
|
||||||
memcpy(PTR_AT(ptr, arr->len * elem_size, char), value, elem_size); |
|
||||||
arr->len++; |
|
||||||
return true; |
|
||||||
} |
|
||||||
|
|
||||||
UPB_INLINE bool _upb_has_field(const void *msg, size_t idx) { |
|
||||||
return (*PTR_AT(msg, idx / 8, const char) & (1 << (idx % 8))) != 0; |
|
||||||
} |
|
||||||
|
|
||||||
UPB_INLINE bool _upb_sethas(const void *msg, size_t idx) { |
|
||||||
return (*PTR_AT(msg, idx / 8, char)) |= (char)(1 << (idx % 8)); |
|
||||||
} |
|
||||||
|
|
||||||
UPB_INLINE bool _upb_clearhas(const void *msg, size_t idx) { |
|
||||||
return (*PTR_AT(msg, idx / 8, char)) &= (char)(~(1 << (idx % 8))); |
|
||||||
} |
|
||||||
|
|
||||||
UPB_INLINE bool _upb_has_oneof_field(const void *msg, size_t case_ofs, int32_t num) { |
|
||||||
return *PTR_AT(msg, case_ofs, int32_t) == num; |
|
||||||
} |
|
||||||
|
|
||||||
#undef PTR_AT |
|
||||||
|
|
||||||
#include "upb/port_undef.inc" |
|
||||||
|
|
||||||
#endif /* UPB_GENERATED_UTIL_H_ */ |
|
Loading…
Reference in new issue