more tests

This commit is contained in:
cozis
2022-04-02 20:23:37 +02:00
parent b8b40f6c54
commit b2ea86585f
2 changed files with 50 additions and 12 deletions
+27 -3
View File
@@ -16,6 +16,21 @@ static const struct {
TEST("\t\n true \t\n"),
TEST("false"),
TEST("\t\n false \t\n"),
TEST("1"),
TEST(" 1 "),
TEST("9999999999999999999999999999999999999999999"),
TEST("[]"),
TEST("[1, 2, 3]"),
TEST(" [ ] "),
TEST(" [ 1 , 2 , 3 ] "),
TEST("{}"),
TEST(" { } "),
TEST(" { \"key\" : 5 } "),
};
#undef TEST
@@ -26,8 +41,14 @@ _Bool json_strings_match(const char *A, const char *B)
do
{
while(isspace(A[Ai])) Ai += 1;
while(isspace(B[Bi])) Bi += 1;
// TODO: Only ignore spaces if they're
// not inside strings.
while(isspace(A[Ai]))
Ai += 1;
while(isspace(B[Bi]))
Bi += 1;
if(A[Ai] != B[Bi]) return 0;
Ai += 1;
Bi += 1;
@@ -68,7 +89,10 @@ int main()
}
else
{
//fprintf(stderr, "Failed.\n");
fprintf(stderr, "Failed! Expected:\n");
fprintf(stderr, " %s\n", tests[i].src);
fprintf(stderr, "but got:\n");
fprintf(stderr, " %s\n", serialized);
}
free(serialized);
+23 -9
View File
@@ -381,14 +381,18 @@ static xj_value *parseArray(context_t *ctx)
if(child == NULL)
return NULL;
// Skip whitespace.
while(ctx->i < ctx->len && isspace(ctx->str[ctx->i]))
ctx->i += 1;
if(ctx->i == ctx->len)
{
xj_report(ctx->error, "String ended inside an array, right after the %dth child", count+1);
return NULL;
}
(*tail)->next = child;
*tail = child;
tail = &child->next;
count += 1;
if(ctx->str[ctx->i] == ']')
@@ -481,10 +485,20 @@ static xj_value *parseObject(context_t *ctx)
if(child == NULL)
return NULL;
// Skip whitespace.
while(ctx->i < ctx->len && isspace(ctx->str[ctx->i]))
ctx->i += 1;
if(ctx->i == ctx->len)
{
xj_report(ctx->error, "String ended inside an object, right after the %dth child", count+1);
return NULL;
}
child->key = key;
(*tail)->next = child;
*tail = child;
tail = &child->next;
count += 1;
if(ctx->str[ctx->i] == '}')
@@ -682,7 +696,7 @@ static _Bool encodeValue(xj_value *val, buffer_t *buff)
assert(k >= 0 && k < (int) sizeof(temp));
if(!appendString(buff, temp, k))
return 0;
break;
return 1;
}
case XJ_FLOAT:
@@ -693,7 +707,7 @@ static _Bool encodeValue(xj_value *val, buffer_t *buff)
assert(k >= 0 && k < (int) sizeof(temp));
if(!appendString(buff, temp, k))
return 0;
break;
return 1;
}
case XJ_ARRAY:
@@ -716,7 +730,7 @@ static _Bool encodeValue(xj_value *val, buffer_t *buff)
if(!appendString(buff, "]", 1))
return 0;
break;
return 1;
}
case XJ_OBJECT:
@@ -727,7 +741,7 @@ static _Bool encodeValue(xj_value *val, buffer_t *buff)
xj_value *child = val->as_object;
while(child != NULL)
{
if(!encodeString(child->key, -1, buff))
if(!encodeString(child->key, strlen(child->key), buff))
return 0;
if(!appendString(buff, ": ", 2))
@@ -745,7 +759,7 @@ static _Bool encodeValue(xj_value *val, buffer_t *buff)
if(!appendString(buff, "}", 1))
return 0;
break;
return 1;
}
case XJ_STRING:
@@ -762,11 +776,11 @@ char *xj_encode(xj_value *value, int *len)
buff.tail = &buff.head;
buff.head.next = NULL;
_Bool failed = !encodeValue(value, &buff);
_Bool ok = encodeValue(value, &buff);
char *serialized = NULL;
if(!failed)
if(ok)
{
/* Serialize */