Browse Source

Increased test coverage to 100%

Sergey Lyubka 11 years ago
parent
commit
cbeaeb218d
2 changed files with 13 additions and 2 deletions
  1. 1 1
      frozen.c
  2. 12 1
      unit_test.c

+ 1 - 1
frozen.c

@@ -266,7 +266,7 @@ static int path_part_len(const char *p) {
 
 const struct json_token *find_json_token(const struct json_token *toks,
                                          const char *path) {
-  if (path == 0 && path[0] == '\0') return 0;
+  if (path == 0 || path[0] == '\0') return 0;
   for (;;) {
     int i, ind2 = 0, ind = -1, skip = 2, n = path_part_len(path);
     if (path[0] == '\0') return 0;

+ 12 - 1
unit_test.c

@@ -15,6 +15,13 @@
 // Alternatively, you can license this library under a commercial
 // license, as set out in <http://cesanta.com/products.html>.
 
+// To unit test on Mac system, do
+//
+// g++ unit_test.c -o unit_test -W -Wall -g -O0 -fprofile-arcs -ftest-coverage
+// clang unit_test.c -o unit_test -W -Wall -g -O0 -fprofile-arcs -ftest-coverage
+// ./unit_test
+// gcov -a unit_test.c
+
 #include "frozen.c"
 
 #include <stdlib.h>
@@ -50,11 +57,13 @@ static const char *test_errors(void) {
   int size = ARRAY_SIZE(ar);
   static const char *invalid_tests[] = {
     "1", "a:3", "\x01", "{:", " { 1", "{a:\"\n\"}", "{a:1x}", "{a:1e}",
-    "{a:.1}", "{a:0.}", "{a:0.e}", "{a:0.e1}", "{a:0.1e}",
+    "{a:.1}", "{a:0.}", "{a:0.e}", "{a:0.e1}", "{a:0.1e}", "{a:\"\\u\"}",
+    "{a:\"\\yx\"}",
     NULL
   };
   static const char *incomplete_tests[] = {
     "", " \r\n\t", "{", " { a", "{a:", "{a:\"", " { a : \"xx", "{a:12",
+    "{a:\"\\uf",
     NULL
   };
   static const struct { const char *str; int expected_len; } success_tests[] = {
@@ -138,6 +147,8 @@ static const char *test_errors(void) {
   ASSERT(find_json_token(ar, "g.h[0]") == &ar[22]);
   ASSERT(find_json_token(ar, "g.h[1]") == NULL);
   ASSERT(find_json_token(ar, "g.h1") == NULL);
+  ASSERT(find_json_token(ar, "") == NULL);
+  ASSERT(find_json_token(ar, NULL) == NULL);
 
   return NULL;
 }