Dash Core  0.12.2.1
P2P Digital Currency
unitester.cpp
Go to the documentation of this file.
1 // Copyright 2014 BitPay Inc.
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <cassert>
9 #include <string>
10 #include "univalue.h"
11 
12 #ifndef JSON_TEST_SRC
13 #error JSON_TEST_SRC must point to test source directory
14 #endif
15 
16 #ifndef ARRAY_SIZE
17 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
18 #endif
19 
20 using namespace std;
21 string srcdir(JSON_TEST_SRC);
22 static bool test_failed = false;
23 
24 #define d_assert(expr) { if (!(expr)) { test_failed = true; fprintf(stderr, "%s failed\n", filename.c_str()); } }
25 #define f_assert(expr) { if (!(expr)) { test_failed = true; fprintf(stderr, "%s failed\n", __func__); } }
26 
27 static std::string rtrim(std::string s)
28 {
29  s.erase(s.find_last_not_of(" \n\r\t")+1);
30  return s;
31 }
32 
33 static void runtest(string filename, const string& jdata)
34 {
35  string prefix = filename.substr(0, 4);
36 
37  bool wantPass = (prefix == "pass") || (prefix == "roun");
38  bool wantFail = (prefix == "fail");
39  bool wantRoundTrip = (prefix == "roun");
40  assert(wantPass || wantFail);
41 
42  UniValue val;
43  bool testResult = val.read(jdata);
44 
45  if (wantPass) {
46  d_assert(testResult == true);
47  } else {
48  d_assert(testResult == false);
49  }
50 
51  if (wantRoundTrip) {
52  std::string odata = val.write(0, 0);
53  assert(odata == rtrim(jdata));
54  }
55 }
56 
57 static void runtest_file(const char *filename_)
58 {
59  string basename(filename_);
60  string filename = srcdir + "/" + basename;
61  FILE *f = fopen(filename.c_str(), "r");
62  assert(f != NULL);
63 
64  string jdata;
65 
66  char buf[4096];
67  while (!feof(f)) {
68  int bread = fread(buf, 1, sizeof(buf), f);
69  assert(!ferror(f));
70 
71  string s(buf, bread);
72  jdata += s;
73  }
74 
75  assert(!ferror(f));
76  fclose(f);
77 
78  runtest(basename, jdata);
79 }
80 
81 static const char *filenames[] = {
82  "fail10.json",
83  "fail11.json",
84  "fail12.json",
85  "fail13.json",
86  "fail14.json",
87  "fail15.json",
88  "fail16.json",
89  "fail17.json",
90  //"fail18.json", // investigate
91  "fail19.json",
92  "fail1.json",
93  "fail20.json",
94  "fail21.json",
95  "fail22.json",
96  "fail23.json",
97  "fail24.json",
98  "fail25.json",
99  "fail26.json",
100  "fail27.json",
101  "fail28.json",
102  "fail29.json",
103  "fail2.json",
104  "fail30.json",
105  "fail31.json",
106  "fail32.json",
107  "fail33.json",
108  "fail34.json",
109  "fail35.json",
110  "fail36.json",
111  "fail37.json",
112  "fail38.json", // invalid unicode: only first half of surrogate pair
113  "fail39.json", // invalid unicode: only second half of surrogate pair
114  "fail40.json", // invalid unicode: broken UTF-8
115  "fail41.json", // invalid unicode: unfinished UTF-8
116  "fail3.json",
117  "fail4.json", // extra comma
118  "fail5.json",
119  "fail6.json",
120  "fail7.json",
121  "fail8.json",
122  "fail9.json", // extra comma
123  "pass1.json",
124  "pass2.json",
125  "pass3.json",
126  "round1.json", // round-trip test
127  "round2.json", // unicode
128 };
129 
130 // Test \u handling
132 {
133  UniValue val;
134  bool testResult;
135  // Escaped ASCII (quote)
136  testResult = val.read("[\"\\u0022\"]");
137  f_assert(testResult);
138  f_assert(val[0].get_str() == "\"");
139  // Escaped Basic Plane character, two-byte UTF-8
140  testResult = val.read("[\"\\u0191\"]");
141  f_assert(testResult);
142  f_assert(val[0].get_str() == "\xc6\x91");
143  // Escaped Basic Plane character, three-byte UTF-8
144  testResult = val.read("[\"\\u2191\"]");
145  f_assert(testResult);
146  f_assert(val[0].get_str() == "\xe2\x86\x91");
147  // Escaped Supplementary Plane character U+1d161
148  testResult = val.read("[\"\\ud834\\udd61\"]");
149  f_assert(testResult);
150  f_assert(val[0].get_str() == "\xf0\x9d\x85\xa1");
151 }
152 
153 int main (int argc, char *argv[])
154 {
155  for (unsigned int fidx = 0; fidx < ARRAY_SIZE(filenames); fidx++) {
156  runtest_file(filenames[fidx]);
157  }
158 
160 
161  return test_failed ? 1 : 0;
162 }
163 
int main(int argc, char *argv[])
Definition: unitester.cpp:153
#define ARRAY_SIZE(arr)
Definition: unitester.cpp:17
static void runtest(string filename, const string &jdata)
Definition: unitester.cpp:33
const char * prefix
Definition: rest.cpp:600
static const char * filenames[]
Definition: unitester.cpp:81
static void runtest_file(const char *filename_)
Definition: unitester.cpp:57
#define d_assert(expr)
Definition: unitester.cpp:24
void unescape_unicode_test()
Definition: unitester.cpp:131
string srcdir(JSON_TEST_SRC)
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
static bool test_failed
Definition: unitester.cpp:22
static std::string rtrim(std::string s)
Definition: unitester.cpp:27
#define f_assert(expr)
Definition: unitester.cpp:25
bool read(const char *raw)