Skip to content

Commit

Permalink
switched to C unit-tests instead of bash
Browse files Browse the repository at this point in the history
  • Loading branch information
Serge A. Zaitsev committed Feb 1, 2012
1 parent 2928f7e commit 4b5c5ed
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 279 deletions.
11 changes: 7 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@

all: libjsmn.a

demo: libjsmn.a demo.o
$(CC) $(LDFLAGS) demo.o -L. -ljsmn -o $@
#demo: libjsmn.a demo.o
# $(CC) $(LDFLAGS) demo.o -L. -ljsmn -o $@

libjsmn.a: jsmn.o
$(AR) rc $@ $^

%.o: %.c jsmn.h
$(CC) -c $(CFLAGS) $< -o $@

test: all demo
sh test.sh
test: jsmn_test
./jsmn_test

jsmn_test: jsmn_test.o
$(CC) -L. -ljsmn $< -o $@

clean:
rm -f jsmn.o demo.o
Expand Down
141 changes: 0 additions & 141 deletions demo.c

This file was deleted.

57 changes: 57 additions & 0 deletions jsmn_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <stdio.h>

#include "jsmn.c"

static int test_passed = 0;
static int test_failed = 0;

/* Terminate current test with error */
#define fail() return __LINE__

/* Successfull end of the test case */
#define done() return 0

/* Check single condition */
#define check(cond) do { if (!(cond)) fail(); } while (0)

/* Test runner */
static void test(int (*func)(void), const char *name) {
int r = func();
if (r == 0) {
test_passed++;
} else {
test_failed++;
printf("FAILED: %s (at line %d)\n", name, r);
}
}

#define TOKEN_EQ(t, tok_start, tok_end, tok_type) \
((t).start == tok_start \
&& (t).end == tok_end \
&& (t).type == (tok_type))

#define TOKEN_PRINT(t) \
printf("start: %d, end: %d, type: %d\n", (t).start, (t).end, (t).type)



int test_primitive() {
int r;
jsmn_parser p;
jsmntok_t tokens[10];

jsmn_init_parser(&p, "{\"a\": 0}", tokens, 10);

r = jsmn_parse(&p);
check(r == JSMN_SUCCESS);
check(TOKEN_EQ(tokens[0], 0, 8, JSMN_OBJECT));
check(TOKEN_EQ(tokens[1], 2, 3, JSMN_STRING));
check(TOKEN_EQ(tokens[2], 6, 7, JSMN_PRIMITIVE));

return 0;
}

int main() {
test(test_primitive, "test primitive values");
return 0;
}
134 changes: 0 additions & 134 deletions test.sh

This file was deleted.

0 comments on commit 4b5c5ed

Please sign in to comment.