yajltest.cpp 4.93 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include "perftest.h"

#if TEST_YAJL

extern "C" {
#include "yajl/yajl_gen.h"
#include "yajl/yajl_parse.h"
#include "yajl/yajl_tree.h"
};

class Yajl : public PerfTest {
public:
	virtual void SetUp() {
		PerfTest::SetUp();
		root_ = yajl_tree_parse(json_, NULL, 0);
		ASSERT_TRUE(root_ != NULL);
	}

	virtual void TearDown() {
		PerfTest::TearDown();
		yajl_tree_free(root_);
	}

protected:
	yajl_val root_;
};

static int null_null(void *) { return 1; }
static int null_boolean(void *, int) { return 1; }
static int null_integer(void *, long long) { return 1; }
static int null_double(void *, double) { return 1; }
static int null_string(void *, const unsigned char*, size_t) { return 1; }
static int null_start_map(void *) { return 1; }
static int null_map_key(void *, const unsigned char*, size_t) { return 1; }
static int null_end_map(void *) { return 1; }
static int null_start_array(void*) { return 1; }
static int null_end_array(void *) { return 1; }

static yajl_callbacks nullcallbacks = {
	null_null,
	null_boolean,
	null_integer,
	null_double,
	NULL,			// yajl_number(). Here we want to test full-parsing performance.
	null_string,
	null_start_map,
	null_map_key,
	null_end_map,
	null_start_array,
	null_end_array
};

TEST_F(Yajl, yajl_parse_nullcallbacks) {
	for (int i = 0; i < kTrialCount; i++) {
		yajl_handle hand = yajl_alloc(&nullcallbacks, NULL, NULL);
56
		yajl_status stat = yajl_parse(hand, (unsigned char*)json_, length_);
57 58
		//ASSERT_EQ(yajl_status_ok, stat);
		if (stat != yajl_status_ok) {
59
			unsigned char * str = yajl_get_error(hand, 1, (unsigned char*)json_, length_);
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
			fprintf(stderr, "%s", (const char *) str);
		}
		stat = yajl_complete_parse(hand);
		ASSERT_EQ(yajl_status_ok, stat);
		yajl_free(hand);
	}	
}

TEST_F(Yajl, yajl_tree_parse) {
	for (int i = 0; i < kTrialCount; i++) {
		yajl_val root = yajl_tree_parse(json_, NULL, 0);
		ASSERT_TRUE(root != NULL);
		yajl_tree_free(root);
	}
}

yajl_gen_status GenVal(yajl_gen g, yajl_val v) {
	yajl_gen_status status;
	switch (v->type) {
	case yajl_t_string:	return yajl_gen_string(g, (unsigned char*)v->u.string, strlen(v->u.string));

	case yajl_t_number: 
		{
			char buffer[100];
			char *num = buffer;
			size_t len;
			//if (YAJL_IS_INTEGER(v)) // buggy
			if (v->u.number.flags & YAJL_NUMBER_INT_VALID)
88 89 90 91 92
#if _MSC_VER
				len = sprintf(num, "%I64d", YAJL_GET_INTEGER(v));
#else
				len = sprintf(num, "%lld", YAJL_GET_INTEGER(v));
#endif
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
			//else if (YAJL_IS_DOUBLE(v))	// buggy
			else if (v->u.number.flags & YAJL_NUMBER_DOUBLE_VALID)
				len = sprintf(num, "%g", YAJL_GET_DOUBLE(v));
			else {
				num = YAJL_GET_NUMBER(v);
				len = strlen(buffer);
			}
			return yajl_gen_number(g, num, len);
		}

	case yajl_t_object:
		status = yajl_gen_map_open(g);
		if (status != yajl_gen_status_ok)
			return status;
		
		for (size_t i = 0; i < v->u.object.len; i++) {
			status = yajl_gen_string(g, (unsigned char *)v->u.object.keys[i], strlen(v->u.object.keys[i]));
			if (status != yajl_gen_status_ok)
				return status;
			status = GenVal(g, v->u.object.values[i]);
			if (status != yajl_gen_status_ok)
				return status;
		}
		return yajl_gen_map_close(g);

	case yajl_t_array:
		status = yajl_gen_array_open(g);
		if (status != yajl_gen_status_ok)
			return status;
		
		for (size_t i = 0; i < v->u.array.len; i++) {
			status = GenVal(g, v->u.array.values[i]);
			if (status != yajl_gen_status_ok)
				return status;
		}

		return yajl_gen_array_close(g);

	case yajl_t_true: return yajl_gen_bool(g, 1);
	case yajl_t_false: return yajl_gen_bool(g, 0);
	case yajl_t_null: return yajl_gen_null(g);
	}
	return yajl_gen_in_error_state;
}

TEST_F(Yajl, yajl_gen) {
	for (int i = 0; i < kTrialCount; i++) {
		yajl_gen g = yajl_gen_alloc(NULL);

		yajl_gen_status status = GenVal(g, root_);
		if (status != yajl_gen_status_ok) {
			std::cout << "gen error: " << status << std::endl;
			FAIL();
		}

		const unsigned char * buf;
		size_t len;
		status = yajl_gen_get_buf(g, &buf, &len);
		ASSERT_EQ(yajl_gen_status_ok, status);
		//if (i == 0)
		//	std::cout << len << std::endl;
		yajl_gen_free(g);
	}	
}

TEST_F(Yajl, yajl_gen_beautify) {
	for (int i = 0; i < kTrialCount; i++) {
		yajl_gen g = yajl_gen_alloc(NULL);
		yajl_gen_config(g, yajl_gen_beautify, 1);
		yajl_gen_config(g, yajl_gen_indent_string, " ");

		yajl_gen_status status = GenVal(g, root_);
		if (status != yajl_gen_status_ok) {
			std::cout << "gen error: " << status << std::endl;
			FAIL();
		}

		const unsigned char * buf;
		size_t len;
		status = yajl_gen_get_buf(g, &buf, &len);
		ASSERT_EQ(yajl_gen_status_ok, status);
		//if (i == 0)
		//	std::cout << len << std::endl;
		yajl_gen_free(g);
	}	
}

TEST_F(Yajl, Whitespace) {
	for (int i = 0; i < kTrialCount; i++) {
		yajl_val root = yajl_tree_parse(whitespace_, NULL, 0);
		ASSERT_TRUE(root != NULL);
		yajl_tree_free(root);
	}
}

#endif // TEST_YAJL