static void test_writer_compound(void) { const char *type = "(uaum(s)u)"; unsigned int u1, u2, u3, u4, u5, u6; const char *s1; CVariant *cv; int r; /* allocate variant and write each entry sequentially */ r = c_variant_new(&cv, type, strlen(type)); assert(r >= 0); r = c_variant_begin(cv, "("); assert(r >= 0); r = c_variant_write(cv, "u", 0xffff); assert(r >= 0); r = c_variant_write(cv, "au", 4, 1, 2, 3, 4); assert(r >= 0); r = c_variant_write(cv, "m(s)", true, "foo"); assert(r >= 0); r = c_variant_write(cv, "u", 0xffffffffU); assert(r >= 0); r = c_variant_end(cv, ")"); assert(r >= 0); /* seal and verify */ r = c_variant_seal(cv); assert(r >= 0); r = c_variant_read(cv, "(uaum(s)u)", &u1, 4, &u2, &u3, &u4, &u5, true, &s1, &u6); assert(r >= 0); assert(u1 == 0xffff); assert(u2 == 1); assert(u3 == 2); assert(u4 == 3); assert(u5 == 4); assert(!strcmp(s1, "foo")); assert(u6 == 0xffffffffU); cv = c_variant_free(cv); assert(!cv); }
static void test_writer_basic(void) { const char *type; unsigned int u1; CVariant *cv; int r; /* simple 'u' type */ type = "u"; r = c_variant_new(&cv, type, strlen(type)); assert(r >= 0); u1 = 0xf0f0; r = c_variant_write(cv, "u", u1); assert(r >= 0); r = c_variant_seal(cv); assert(r >= 0); u1 = 0; r = c_variant_read(cv, "u", &u1); assert(r >= 0); assert(u1 == 0xf0f0); cv = c_variant_free(cv); /* compound '(u)' type */ type = "(u)"; r = c_variant_new(&cv, type, strlen(type)); assert(r >= 0); u1 = 0xf0f0; r = c_variant_write(cv, "(u)", u1); assert(r >= 0); r = c_variant_seal(cv); assert(r >= 0); u1 = 0; r = c_variant_read(cv, "(u)", &u1); assert(r >= 0); assert(u1 == 0xf0f0); cv = c_variant_free(cv); /* array 'au' type */ type = "au"; r = c_variant_new(&cv, type, strlen(type)); assert(r >= 0); u1 = 0xf0f0; r = c_variant_write(cv, "au", 1, u1); assert(r >= 0); r = c_variant_seal(cv); assert(r >= 0); u1 = 0; r = c_variant_read(cv, "au", 1, &u1); assert(r >= 0); assert(u1 == 0xf0f0); cv = c_variant_free(cv); /* maybe 'mu' type */ type = "mu"; r = c_variant_new(&cv, type, strlen(type)); assert(r >= 0); u1 = 0xf0f0; r = c_variant_write(cv, "mu", true, u1); assert(r >= 0); r = c_variant_seal(cv); assert(r >= 0); u1 = 0; r = c_variant_read(cv, "mu", true, &u1); assert(r >= 0); assert(u1 == 0xf0f0); cv = c_variant_free(cv); /* variant 'v', 'u' */ type = "v"; r = c_variant_new(&cv, type, strlen(type)); assert(r >= 0); u1 = 0xf0f0; r = c_variant_write(cv, "v", "u", u1); assert(r >= 0); r = c_variant_seal(cv); assert(r >= 0); u1 = 0; r = c_variant_read(cv, "v", "u", &u1); assert(r >= 0); assert(u1 == 0xf0f0); cv = c_variant_free(cv); }
static void test_print_cv(CVariant *cv) { CVariantVarg varg; const char *type, *s; uint64_t u64; uint32_t u32; uint16_t u16; uint8_t u8; double f64; int r, c, nest; size_t n; nest = 0; type = c_variant_peek_type(cv, &n); for (c = c_variant_varg_init(&varg, type, n); c; c = c_variant_varg_next(&varg)) { switch (c) { case -1: c_variant_exit(cv, NULL); assert(nest-- > 0); printf("%*s}\n", nest * 4, ""); break; case C_VARIANT_VARIANT: c_variant_enter(cv, "v"); type = c_variant_peek_type(cv, &n); c_variant_varg_push(&varg, type, n, -1); printf("%*sv - \"%.*s\" {\n", nest * 4, "", (int)n, type); ++nest; break; case C_VARIANT_MAYBE: c_variant_enter(cv, "m"); n = c_variant_peek_count(cv); c_variant_varg_enter_bound(&varg, cv, n); printf("%*sMAYBE - %s {\n", nest * 4, "", n ? "SOMETHING" : "NOTHING"); ++nest; break; case C_VARIANT_ARRAY: c_variant_enter(cv, "a"); n = c_variant_peek_count(cv); c_variant_varg_enter_bound(&varg, cv, n); printf("%*sARRAY - %zu {\n", nest * 4, "", n); ++nest; break; case C_VARIANT_TUPLE_OPEN: c_variant_enter(cv, "("); c_variant_varg_enter_unbound(&varg, cv, ')'); type = c_variant_peek_type(cv, &n); printf("%*s\"(%.*s)\" {\n", nest * 4, "", (int)n, type); ++nest; break; case C_VARIANT_PAIR_OPEN: c_variant_enter(cv, "{"); c_variant_varg_enter_unbound(&varg, cv, '}'); type = c_variant_peek_type(cv, &n); printf("%*s\"{%.*s}\" {\n", nest * 4, "", (int)n, type); ++nest; break; case C_VARIANT_INT64: c_variant_read(cv, "x", &u64); printf("%*sINT64: %"PRId64"\n", nest * 4, "", u64); break; case C_VARIANT_UINT64: c_variant_read(cv, "t", &u64); printf("%*sUINT64: %"PRIu64"\n", nest * 4, "", u64); break; case C_VARIANT_DOUBLE: c_variant_read(cv, "d", &f64); printf("%*sDOUBLE: %f\n", nest * 4, "", f64); break; case C_VARIANT_INT32: c_variant_read(cv, "i", &u32); printf("%*sINT32: %"PRId32"\n", nest * 4, "", u32); break; case C_VARIANT_UINT32: c_variant_read(cv, "u", &u32); printf("%*sUINT32: %"PRIu32"\n", nest * 4, "", u32); break; case C_VARIANT_HANDLE: c_variant_read(cv, "h", &u32); printf("%*sHANDLE: %"PRIu32"\n", nest * 4, "", u32); break; case C_VARIANT_INT16: c_variant_read(cv, "n", &u16); printf("%*sINT16: %"PRId16"\n", nest * 4, "", u16); break; case C_VARIANT_UINT16: c_variant_read(cv, "q", &u16); printf("%*sUINT16: %"PRIu16"\n", nest * 4, "", u16); break; case C_VARIANT_BOOL: c_variant_read(cv, "b", &u8); printf("%*sBOOL: %s\n", nest * 4, "", u8 ? "TRUE" : "FALSE"); break; case C_VARIANT_BYTE: c_variant_read(cv, "y", &u8); printf("%*sBYTE: %d\n", nest * 4, "", u8); break; case C_VARIANT_STRING: c_variant_read(cv, "s", &s); printf("%*sSTRING: %s\n", nest * 4, "", s); break; case C_VARIANT_PATH: c_variant_read(cv, "o", &s); printf("%*sPATH: %s\n", nest * 4, "", s); break; case C_VARIANT_SIGNATURE: c_variant_read(cv, "g", &s); printf("%*sSIGNATURE: %s\n", nest * 4, "", s); break; default: fprintf(stderr, "ERR: %d\n", c); assert(0); break; } r = c_variant_return_poison(cv); assert(r >= 0); } }
static void test_compare(CVariant *cv, GVariant *gv) { const GVariantType *gvt; GVariantIter gvi[C_VARIANT_MAX_VARG]; GVariant *gvig[C_VARIANT_MAX_VARG]; CVariantVarg varg; const char *s, *type; uint64_t val_64; uint32_t val_32; uint16_t val_16; uint8_t val_8; double val_f; size_t n, nest; GVariant *g; int r, c; type = c_variant_peek_type(cv, &n); gvt = g_variant_get_type(gv); assert(n == g_variant_type_get_string_length(gvt)); assert(!memcmp(type, g_variant_type_peek_string(gvt), n)); nest = 0; g = gv; for (c = c_variant_varg_init(&varg, type, n); c; c = c_variant_varg_next(&varg)) { if (c == -1) { r = c_variant_exit(cv, NULL); assert(r >= 0); assert(nest-- > 0); g_variant_unref(gvig[nest]); continue; } if (nest > 0) g = g_variant_iter_next_value(&gvi[nest - 1]); else g = g_variant_ref(gv); assert(g); switch (c) { case C_VARIANT_VARIANT: c_variant_enter(cv, "v"); type = c_variant_peek_type(cv, &n); c_variant_varg_push(&varg, type, n, -1); g_variant_iter_init(&gvi[nest], g); gvig[nest] = g_variant_ref(g); ++nest; break; case C_VARIANT_MAYBE: c_variant_enter(cv, "m"); n = c_variant_peek_count(cv); c_variant_varg_enter_bound(&varg, cv, n); g_variant_iter_init(&gvi[nest], g); gvig[nest] = g_variant_ref(g); ++nest; break; case C_VARIANT_ARRAY: c_variant_enter(cv, "a"); n = c_variant_peek_count(cv); c_variant_varg_enter_bound(&varg, cv, n); g_variant_iter_init(&gvi[nest], g); gvig[nest] = g_variant_ref(g); ++nest; break; case C_VARIANT_TUPLE_OPEN: c_variant_enter(cv, "("); c_variant_varg_enter_unbound(&varg, cv, ')'); g_variant_iter_init(&gvi[nest], g); gvig[nest] = g_variant_ref(g); ++nest; break; case C_VARIANT_PAIR_OPEN: c_variant_enter(cv, "{"); c_variant_varg_enter_unbound(&varg, cv, '}'); g_variant_iter_init(&gvi[nest], g); gvig[nest] = g_variant_ref(g); ++nest; break; case C_VARIANT_INT64: c_variant_read(cv, "x", &val_64); assert((int64_t)val_64 == g_variant_get_int64(g)); break; case C_VARIANT_UINT64: c_variant_read(cv, "t", &val_64); assert((uint64_t)val_64 == g_variant_get_uint64(g)); break; case C_VARIANT_DOUBLE: c_variant_read(cv, "d", &val_f); assert(!(val_f > g_variant_get_double(g)) && !(val_f < g_variant_get_double(g))); break; case C_VARIANT_INT32: c_variant_read(cv, "i", &val_32); assert((int32_t)val_32 == g_variant_get_int32(g)); break; case C_VARIANT_UINT32: c_variant_read(cv, "u", &val_32); assert((uint32_t)val_32 == g_variant_get_uint32(g)); break; case C_VARIANT_HANDLE: c_variant_read(cv, "h", &val_32); assert((int32_t)val_32 == g_variant_get_handle(g)); break; case C_VARIANT_INT16: c_variant_read(cv, "n", &val_16); assert((int16_t)val_16 == g_variant_get_int16(g)); break; case C_VARIANT_UINT16: c_variant_read(cv, "q", &val_16); assert((uint16_t)val_16 == g_variant_get_uint16(g)); break; case C_VARIANT_BOOL: c_variant_read(cv, "b", &val_8); assert((bool)val_8 == g_variant_get_boolean(g)); break; case C_VARIANT_BYTE: c_variant_read(cv, "y", &val_8); assert((guchar)val_8 == g_variant_get_byte(g)); break; case C_VARIANT_STRING: c_variant_read(cv, "s", &s); assert(!strcmp(s, g_variant_get_string(g, NULL))); break; case C_VARIANT_PATH: c_variant_read(cv, "o", &s); assert(!strcmp(s, g_variant_get_string(g, NULL))); break; case C_VARIANT_SIGNATURE: c_variant_read(cv, "g", &s); assert(!strcmp(s, g_variant_get_string(g, NULL))); break; default: assert(0); break; } r = c_variant_return_poison(cv); assert(r >= 0); g_variant_unref(g); } }