diff options
author | Joshua Haberman <joshua@reverberate.org> | 2010-07-11 18:53:27 -0700 |
---|---|---|
committer | Joshua Haberman <joshua@reverberate.org> | 2010-07-11 18:53:27 -0700 |
commit | 7a6a702792e769366a8852fc90dbea9cfc9e01c0 (patch) | |
tree | 3315b7ffaf527f1096aba986a73d346be00a68a1 /tests/test_string.c | |
parent | fcfc37e7d41f87bc9ff5ecfb64e0aebb3457c633 (diff) |
Allow static upb_strings.
This can allow strings to reference static data, and
reduced the memory footprint of test_def by about 10%
(3k).
Diffstat (limited to 'tests/test_string.c')
-rw-r--r-- | tests/test_string.c | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/tests/test_string.c b/tests/test_string.c index 46f35b9..7c9ed02 100644 --- a/tests/test_string.c +++ b/tests/test_string.c @@ -3,8 +3,33 @@ #include "upb_string.h" char static_str[] = "Static string."; +upb_string static_upbstr = UPB_STATIC_STRING(static_str); -int main() { +static void test_static() { + // Static string is initialized appropriately. + assert(upb_streql(&static_upbstr, UPB_STRLIT("Static string."))); + + // Taking a ref on a static string returns the same string, and repeated + // refs don't get the string in a confused state. + assert(upb_string_getref(&static_upbstr) == &static_upbstr); + assert(upb_string_getref(&static_upbstr) == &static_upbstr); + assert(upb_string_getref(&static_upbstr) == &static_upbstr); + + // Unreffing a static string does nothing (is not harmful). + upb_string_unref(&static_upbstr); + upb_string_unref(&static_upbstr); + upb_string_unref(&static_upbstr); + upb_string_unref(&static_upbstr); + upb_string_unref(&static_upbstr); + + // Recycling a static string returns a new string (that can be modified). + upb_string *str = upb_string_tryrecycle(&static_upbstr); + assert(str != &static_upbstr); + + upb_string_unref(str); +} + +static void test_dynamic() { upb_string *str = upb_string_new(); assert(str != NULL); upb_string_unref(str); @@ -29,6 +54,7 @@ int main() { const char *robuf2 = upb_string_getrobuf(str); assert(robuf2 == robuf); assert(upb_streqlc(str, "XX")); + assert(upb_streql(str, UPB_STRLIT("XX"))); // Make string alias part of another string. str2 = upb_strdupc("WXYZ"); @@ -79,3 +105,8 @@ int main() { // Unref of NULL is harmless. upb_string_unref(NULL); } + +int main() { + test_static(); + test_dynamic(); +} |