static int GetValueTest(WJElement doc) { char *v; if (1 != WJENumber(doc, "one", WJE_GET, -1)) return(__LINE__); if (2 != WJENumber(doc, "two", WJE_GET, -1)) return(__LINE__); if (3 != WJENumber(doc, "three", WJE_GET, -1)) return(__LINE__); if (-1!= WJENumber(doc, "four", WJE_GET, -1)) return(__LINE__); if (0 != WJENumber(doc, "digits[0]", WJE_GET, -1)) return(__LINE__); if (1 != WJENumber(doc, "digits[1]", WJE_GET, -1)) return(__LINE__); if (9 != WJENumber(doc, "digits[-1]", WJE_GET, -1)) return(__LINE__); if (9 != WJENumber(doc, "digits[$]", WJE_GET, -1)) return(__LINE__); if (-1!= WJENumber(doc, "digits[10]", WJE_GET, -1)) return(__LINE__); if (!WJEBool(doc, "a.b.balloon", WJE_GET, 0)) return(__LINE__); if ( WJEBool(doc, "a.b.aardvark", WJE_GET, 1)) return(__LINE__); if (!WJEBool(doc, "a.b.bull", WJE_GET, 1)) return(__LINE__); if ( WJEBool(doc, "a.b.another", WJE_GET, 0)) return(__LINE__); if (!(v = WJEString(doc, "a.names[-1]", WJE_GET, NULL)) || stricmp(v, "a")) return(__LINE__); if (!(v = WJEString(doc, "a.b.names[-1]", WJE_GET, NULL)) || stricmp(v, "b")) return(__LINE__); if ( (v = WJEString(doc, "a.b.nonames[-1]", WJE_GET, NULL))) return(__LINE__); return(0); }
static int NewValueTest(WJElement doc) { char *v; /* WJE_NEW will not leave the original value if it already exists, or create the element if it doesn't. Do a WJE_NEW on values that do and do not already exist, and verify the result. Then do a WJE_GET to verify that the creation of elements actually worked. */ /* Replace existing number values */ if ( 1!= WJENumber(doc, "one", WJE_NEW, -1)) return(__LINE__); if ( 2!= WJENumber(doc, "two", WJE_NEW, -1)) return(__LINE__); if ( 3!= WJENumber(doc, "three", WJE_NEW, -1)) return(__LINE__); /* Insert a new number value */ if (-1!= WJENumber(doc, "four", WJE_NEW, -1)) return(__LINE__); if (-1!= WJENumber(doc, "four", WJE_GET, 4)) return(__LINE__); /* Replace existing boolean values */ if (!WJEBool(doc, "a.b.balloon", WJE_NEW, 0)) return(__LINE__); if ( WJEBool(doc, "a.b.aardvark", WJE_NEW, 1)) return(__LINE__); /* Insert a new boolean value */ if (!WJEBool(doc, "a.b.bull", WJE_NEW, 1)) return(__LINE__); if (!WJEBool(doc, "a.b.bull", WJE_GET, 0)) return(__LINE__); /* Replace an existing string value */ if (!(v = WJEString(doc, "a.names[-1]", WJE_NEW, "aardvark")) || stricmp(v, "a")) return(__LINE__); if (!(v = WJEString(doc, "a.names[-1]", WJE_GET, NULL)) || stricmp(v, "a")) return(__LINE__); /* Insert a new string value */ if (!(v = WJEString(doc, "a.x.q", WJE_NEW, "queen")) || stricmp(v, "queen")) return(__LINE__); if (!(v = WJEString(doc, "a.x.q", WJE_GET, NULL)) || stricmp(v, "queen")) return(__LINE__); return(0); }
static int SetValueTest(WJElement doc) { char *v; /* WJE_SET will update the value if it already exists, or create the element if it doesn't exist. Do a WJE_SET on values that do and do not already exist, and verify the result. Then do a WJE_GET to verify that the element does exist. */ /* Replace existing number values */ if (-1!= WJENumber(doc, "one", WJE_SET, -1)) return(__LINE__); if (-1!= WJENumber(doc, "one", WJE_GET, 1)) return(__LINE__); if (-1!= WJENumber(doc, "two", WJE_SET, -1)) return(__LINE__); if (-1!= WJENumber(doc, "two", WJE_GET, 2)) return(__LINE__); if (-1!= WJENumber(doc, "three", WJE_SET, -1)) return(__LINE__); if (-1!= WJENumber(doc, "three", WJE_GET, 3)) return(__LINE__); /* Insert a new number value */ if (-1!= WJENumber(doc, "four", WJE_SET, -1)) return(__LINE__); if (-1!= WJENumber(doc, "four", WJE_GET, 4)) return(__LINE__); /* Replace existing boolean values */ if ( WJEBool(doc, "a.b.balloon", WJE_SET, 0)) return(__LINE__); if ( WJEBool(doc, "a.b.balloon", WJE_GET, 1)) return(__LINE__); if (!WJEBool(doc, "a.b.aardvark", WJE_SET, 1)) return(__LINE__); if (!WJEBool(doc, "a.b.aardvark", WJE_GET, 0)) return(__LINE__); /* Insert a new boolean value */ if (!WJEBool(doc, "a.b.bull", WJE_SET, 1)) return(__LINE__); if (!WJEBool(doc, "a.b.bull", WJE_GET, 0)) return(__LINE__); /* Replace an existing string value */ if (!(v = WJEString(doc, "a.names[-1]", WJE_SET, "aardvark"))) return(__LINE__); if (!(v = WJEString(doc, "a.names[-1]", WJE_GET, NULL)) || stricmp(v, "aardvark")) return(__LINE__); /* Insert a new string value */ if (!(v = WJEString(doc, "a.x.q", WJE_SET, "queen"))) return(__LINE__); if (!(v = WJEString(doc, "a.x.q", WJE_GET, NULL)) || stricmp(v, "queen")) return(__LINE__); return(0); }
static int PutValueTest(WJElement doc) { char *v; /* WJE_PUT will update the value if the element exists. If it does not exist then it will NOT create it. Do a WJE_PUT on values that do and do not already exist, and verify the result. Then use WJEGet() to verify that new elements where not created. */ /* Replace existing number values */ if (-1!= WJENumber(doc, "one", WJE_PUT, -1)) return(__LINE__); if (-1!= WJENumber(doc, "one", WJE_GET, 1)) return(__LINE__); if (-1!= WJENumber(doc, "two", WJE_PUT, -1)) return(__LINE__); if (-1!= WJENumber(doc, "two", WJE_GET, 2)) return(__LINE__); if (-1!= WJENumber(doc, "three", WJE_PUT, -1)) return(__LINE__); if (-1!= WJENumber(doc, "three", WJE_GET, 3)) return(__LINE__); /* Insert a new number value */ if (-1== WJENumber(doc, "four", WJE_PUT, -1)) return(__LINE__); if (WJEGet(doc, "four", NULL)) return(__LINE__); /* Replace existing boolean values */ if ( WJEBool(doc, "a.b.balloon", WJE_PUT, 0)) return(__LINE__); if (!WJEBool(doc, "a.b.aardvark", WJE_PUT, 1)) return(__LINE__); /* Insert a new boolean value */ if ( WJEBool(doc, "a.b.bull", WJE_PUT, 1)) return(__LINE__); if (WJEGet(doc, "a.b.bull", NULL)) return(__LINE__); /* Replace an existing string value */ if (!(v = WJEString(doc, "a.names[-1]", WJE_PUT, "aardvark")) || stricmp(v, "aardvark")) return(__LINE__); if (!(v = WJEString(doc, "a.names[-1]", WJE_GET, NULL)) || stricmp(v, "aardvark")) return(__LINE__); /* Insert a new string value */ if ((v = WJEString(doc, "a.x.q", WJE_PUT, "queen"))) return(__LINE__); if (WJEGet(doc, "a.x.q", NULL)) return(__LINE__); return(0); }
static void _WJEHash(WJElement document, int depth, WJEHashCB update, void *context) { WJElement child; char *s; int32 n; uint32 b; if (!document) { return; } switch (document->type) { default: case WJR_TYPE_UNKNOWN: break; case WJR_TYPE_NULL: update(context, "", 1); break; case WJR_TYPE_OBJECT: update(context, &depth, sizeof(depth)); for (child = document->child; child; child = child->next) { if (child->name) { update(context, child->name, strlen(child->name) + 1); } _WJEHash(child, depth + 1, update, context); } update(context, &depth, sizeof(depth)); break; case WJR_TYPE_ARRAY: update(context, &depth, sizeof(depth)); for (child = document->child; child; child = child->next) { update(context, "", 1); _WJEHash(child, depth + 1, update, context); } update(context, &depth, sizeof(depth)); break; case WJR_TYPE_STRING: if ((s = WJEString(document, NULL, WJE_GET, ""))) { update(context, s, strlen(s) + 1); } break; case WJR_TYPE_NUMBER: #ifdef WJE_DISTINGUISH_INTEGER_TYPE case WJR_TYPE_INTEGER: #endif n = WJENumber(document, NULL, WJE_GET, 0); update(context, &n, sizeof(n)); break; case WJR_TYPE_TRUE: case WJR_TYPE_BOOL: case WJR_TYPE_FALSE: b = WJEBool(document, NULL, WJE_GET, FALSE); update(context, &b, sizeof(b)); break; } }