Browse Source

Unbroke tests under Windows

Sergey Lyubka 11 years ago
parent
commit
cf4d19e9dd
2 changed files with 22 additions and 7 deletions
  1. 16 4
      frozen.c
  2. 6 3
      unit_test.c

+ 16 - 4
frozen.c

@@ -373,11 +373,17 @@ struct json_token *find_json_token(struct json_token *toks, const char *path) {
 }
 
 int json_emit_long(char *buf, int buf_len, long int value) {
-  return snprintf(buf, buf_len > 0 ? buf_len : 0, "%ld", value);
+  char tmp[20];
+  int n = snprintf(tmp, sizeof(tmp), "%ld", value);
+  strncpy(buf, tmp, buf_len > 0 ? buf_len : 0);
+  return n;
 }
 
 int json_emit_double(char *buf, int buf_len, double value) {
-  return snprintf(buf, buf_len > 0 ? buf_len : 0, "%g", value);
+  char tmp[20];
+  int n = snprintf(tmp, sizeof(tmp), "%g", value);
+  strncpy(buf, tmp, buf_len > 0 ? buf_len : 0);
+  return n;
 }
 
 int json_emit_quoted_str(char *s, int s_len, const char *str, int len) {
@@ -401,14 +407,20 @@ int json_emit_quoted_str(char *s, int s_len, const char *str, int len) {
     }
   }
   EMIT('"');
-  if (s < end) *s = '\0';
+  if (s < end) {
+    *s = '\0';
+  }
 
   return s - begin;
 }
 
 int json_emit_unquoted_str(char *buf, int buf_len, const char *str, int len) {
   if (buf_len > 0 && len > 0) {
-    memcpy(buf, str, len < buf_len ? len : buf_len);
+    int n = len < buf_len ? len : buf_len;
+    memcpy(buf, str, n);
+    if (n < buf_len) {
+      buf[n] = '\0';
+    }
   }
   return len;
 }

+ 6 - 3
unit_test.c

@@ -73,7 +73,7 @@ static const char *test_errors(void) {
   };
   static const struct { const char *str; int expected_len; } success_tests[] = {
     { "{}", 2 },
-    // 2, 3, 4 byte utf-8 chars
+    /* 2, 3, 4 byte utf-8 chars */
     { "{a:\"\xd0\xb1\xe3\x81\xaf\xf0\xa2\xb3\x82\"}", 15 },
     { "{a:\"\\u0006\"}", 12 },
     { " { } ", 4 },
@@ -206,6 +206,7 @@ static const char *test_emit_escapes(void) {
 static const char *test_emit(void) {
   char buf[1000], *p = buf;
   const char *s5 = "{\"foo\":[-123,1.23,true]}";
+  const char *s6 = "{\"foo\":[-7,true, false,null]}";
 
   p += json_emit_unquoted_str(p, &buf[sizeof(buf)] - p, "{", 1);
   p += json_emit_quoted_str(p, &buf[sizeof(buf)] - p, "foo", 3);
@@ -216,17 +217,19 @@ static const char *test_emit(void) {
   p += json_emit_unquoted_str(p, &buf[sizeof(buf)] - p, ",", 1);
   p += json_emit_unquoted_str(p, &buf[sizeof(buf)] - p, "true", 4);
   p += json_emit_unquoted_str(p, &buf[sizeof(buf)] - p, "]}", 2);
+
   ASSERT(strcmp(buf, s5) == 0);
   ASSERT(p < &buf[sizeof(buf)]);
 
   ASSERT(json_emit(buf, sizeof(buf), "{v:[i,f,V]}",
          "foo", 3, (long) -123, 1.23, "true", 4) > 0);
+  ASSERT(strcmp(buf, s5) == 0);
+
   ASSERT(json_emit(buf, 4, "{S:i}", "a", 12345) > 4);
   ASSERT(json_emit(buf, sizeof(buf), "{S:d}", "a", 12345) == 0);
 
   ASSERT(json_emit(buf, sizeof(buf), "{s:[i,T, F,N]}", "foo", (long) -7) > 0);
-  printf("[%s]\n", buf);
-  ASSERT(strcmp(buf, "{\"foo\":[-7,true, false,null]}") == 0);
+  ASSERT(strcmp(buf, s6) == 0);
 
   return NULL;
 }