added basic tests
This commit is contained in:
@@ -1 +1,2 @@
|
|||||||
test
|
test
|
||||||
|
vgcore.*
|
||||||
@@ -1,29 +1,79 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <ctype.h>
|
||||||
#include "xjson.h"
|
#include "xjson.h"
|
||||||
|
|
||||||
|
#define TEST(src_) { .line = __LINE__, .src = src_ }
|
||||||
|
|
||||||
|
static const struct {
|
||||||
|
int line;
|
||||||
|
const char *src;
|
||||||
|
} tests[] = {
|
||||||
|
TEST("null"),
|
||||||
|
TEST("\t\n null \t\n"),
|
||||||
|
TEST("true"),
|
||||||
|
TEST("\t\n true \t\n"),
|
||||||
|
TEST("false"),
|
||||||
|
TEST("\t\n false \t\n"),
|
||||||
|
};
|
||||||
|
|
||||||
|
#undef TEST
|
||||||
|
|
||||||
|
_Bool json_strings_match(const char *A, const char *B)
|
||||||
|
{
|
||||||
|
int Ai = 0, Bi = 0;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
while(isspace(A[Ai])) Ai += 1;
|
||||||
|
while(isspace(B[Bi])) Bi += 1;
|
||||||
|
if(A[Ai] != B[Bi]) return 0;
|
||||||
|
Ai += 1;
|
||||||
|
Bi += 1;
|
||||||
|
}
|
||||||
|
while(A[Ai] != '\0');
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
char pool[4096];
|
char pool[65536];
|
||||||
xj_error error;
|
xj_error error;
|
||||||
xj_alloc *alloc;
|
|
||||||
|
|
||||||
alloc = xj_newAllocUsing(pool, sizeof(pool), 0, NULL);
|
int total = sizeof(tests) / sizeof(tests[0]);
|
||||||
|
int passed = 0;
|
||||||
|
|
||||||
|
for(int i = 0; i < total; i += 1)
|
||||||
|
{
|
||||||
|
xj_alloc *alloc = xj_newAllocUsing(pool, sizeof(pool), 0, NULL);
|
||||||
assert(alloc != NULL);
|
assert(alloc != NULL);
|
||||||
|
|
||||||
xj_value *val = xj_decode("{}", -1, alloc, &error);
|
xj_value *val = xj_decode(tests[i].src, -1, alloc, &error);
|
||||||
|
|
||||||
if(val == NULL)
|
if(val == NULL)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Failed to parse (%s)\n", error.message);
|
fprintf(stderr, "Failed to parse (%s)\n", error.message);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
fprintf(stdout, "Parsed.\n");
|
|
||||||
|
|
||||||
int size;
|
int size;
|
||||||
char *serialized = xj_encode(val, &size);
|
char *serialized = xj_encode(val, &size);
|
||||||
fprintf(stdout, "(%d) %s\n", size, serialized);
|
assert(serialized != NULL);
|
||||||
|
|
||||||
|
if(json_strings_match(serialized, tests[i].src))
|
||||||
|
{
|
||||||
|
//fprintf(stdout, "Passed.\n");
|
||||||
|
passed += 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//fprintf(stderr, "Failed.\n");
|
||||||
|
}
|
||||||
|
|
||||||
free(serialized);
|
free(serialized);
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(stdout, "passed: %d, failed: %d, total: %d\n", passed, total - passed, total);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -476,12 +476,6 @@ static xj_value *parseObject(context_t *ctx)
|
|||||||
while(ctx->i < ctx->len && isspace(ctx->str[ctx->i]))
|
while(ctx->i < ctx->len && isspace(ctx->str[ctx->i]))
|
||||||
ctx->i += 1;
|
ctx->i += 1;
|
||||||
|
|
||||||
if(ctx->i == ctx->len)
|
|
||||||
{
|
|
||||||
xj_report(ctx->error, "String ended inside an object, right before the %dth child's value", count+1);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
xj_value *child = parseValue(ctx);
|
xj_value *child = parseValue(ctx);
|
||||||
|
|
||||||
if(child == NULL)
|
if(child == NULL)
|
||||||
@@ -522,14 +516,14 @@ static xj_value *parseObject(context_t *ctx)
|
|||||||
|
|
||||||
static xj_value *parseValue(context_t *ctx)
|
static xj_value *parseValue(context_t *ctx)
|
||||||
{
|
{
|
||||||
assert(!isspace(ctx->str[ctx->i]));
|
|
||||||
|
|
||||||
if(ctx->i == ctx->len)
|
if(ctx->i == ctx->len)
|
||||||
{
|
{
|
||||||
xj_report(ctx->error, "String ended where a value was expected");
|
xj_report(ctx->error, "String ended where a value was expected");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assert(!isspace(ctx->str[ctx->i]));
|
||||||
|
|
||||||
char c = ctx->str[ctx->i];
|
char c = ctx->str[ctx->i];
|
||||||
|
|
||||||
if(c == '"')
|
if(c == '"')
|
||||||
@@ -570,6 +564,7 @@ static xj_value *parseValue(context_t *ctx)
|
|||||||
|
|
||||||
if(ctx->i + kwlen <= ctx->len && !strncmp(ctx->str + ctx->i, kword, kwlen))
|
if(ctx->i + kwlen <= ctx->len && !strncmp(ctx->str + ctx->i, kword, kwlen))
|
||||||
{
|
{
|
||||||
|
ctx->i += kwlen;
|
||||||
switch(c)
|
switch(c)
|
||||||
{
|
{
|
||||||
case 'n': return xj_newNull(ctx->alloc);
|
case 'n': return xj_newNull(ctx->alloc);
|
||||||
@@ -615,7 +610,7 @@ xj_value *xj_decode(const char *str, int len,
|
|||||||
}
|
}
|
||||||
|
|
||||||
context_t ctx = {
|
context_t ctx = {
|
||||||
.str = str, .len = len,
|
.str = str, .i = i, .len = len,
|
||||||
.alloc = alloc, .error = error };
|
.alloc = alloc, .error = error };
|
||||||
return parseValue(&ctx);
|
return parseValue(&ctx);
|
||||||
}
|
}
|
||||||
@@ -767,7 +762,7 @@ char *xj_encode(xj_value *value, int *len)
|
|||||||
buff.tail = &buff.head;
|
buff.tail = &buff.head;
|
||||||
buff.head.next = NULL;
|
buff.head.next = NULL;
|
||||||
|
|
||||||
_Bool failed = encodeValue(value, &buff);
|
_Bool failed = !encodeValue(value, &buff);
|
||||||
|
|
||||||
char *serialized = NULL;
|
char *serialized = NULL;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user