File: cu_varint.c

package info (click to toggle)
postgis 2.3.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 58,660 kB
  • ctags: 10,181
  • sloc: ansic: 132,858; sql: 131,148; xml: 46,460; sh: 4,832; perl: 4,476; makefile: 2,749; python: 1,198; yacc: 442; lex: 131
file content (240 lines) | stat: -rw-r--r-- 6,797 bytes parent folder | download | duplicates (2)
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
56
57
58
59
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
88
89
90
91
92
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/**********************************************************************
 *
 * PostGIS - Spatial Types for PostgreSQL
 * http://postgis.net
 *
 * Copyright (C) 2013 Nicklas Avén
 *
 * This is free software; you can redistribute and/or modify it under
 * the terms of the GNU General Public Licence. See the COPYING file.
 *
 **********************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "CUnit/Basic.h"
#include "CUnit/CUnit.h"
#include "liblwgeom_internal.h"
#include "varint.h"
#include "cu_tester.h"



// size_t varint_u32_encode_buf(uint32_t val, uint8_t *buf);
// size_t varint_s32_encode_buf(int32_t val, uint8_t *buf);
// size_t varint_u64_encode_buf(uint64_t val, uint8_t *buf);
// size_t varint_s64_encode_buf(int64_t val, uint8_t *buf);
// int64_t varint_s64_decode(const uint8_t *the_start, const uint8_t *the_end, size_t *size);
// uint64_t varint_u64_decode(const uint8_t *the_start, const uint8_t *the_end, size_t *size);
//
// size_t varint_size(const uint8_t *the_start, const uint8_t *the_end);
//

static void do_test_u32_varint(uint32_t nr, int expected_size, char* expected_res)
{
	int size;
	char *hex;
	uint8_t buf[16];
	
	size = varint_u32_encode_buf(nr, buf);
	if ( size != expected_size )
		printf("Expected: %d\nObtained: %d\n", expected_size, size);

	CU_ASSERT_EQUAL(size, expected_size);
	
	hex = hexbytes_from_bytes(buf, size);
	ASSERT_STRING_EQUAL(hex, expected_res);	
	lwfree(hex);
}

static void do_test_s32_varint(int32_t nr,int expected_size, char* expected_res)
{
	uint8_t buf[16];
	int size;
	char *hex;
	
	size = varint_s32_encode_buf(nr, buf);
	if ( size != expected_size )
	{
		printf("Expected: %d\nObtained: %d\n", expected_size, size);
	}
	CU_ASSERT_EQUAL(size,expected_size);

	hex = hexbytes_from_bytes(buf, size);
	ASSERT_STRING_EQUAL(hex, expected_res);	
	lwfree(hex);
}

static void do_test_u64_varint(uint64_t nr,int expected_size, char* expected_res)
{
	uint8_t buf[16];
	int size;
	char *hex;
	
	size = varint_u64_encode_buf(nr, buf);
	if ( size != expected_size )
	{
		printf("Expected: %d\nObtained: %d\n", expected_size, size);
	}
	CU_ASSERT_EQUAL(size,expected_size);

	hex = hexbytes_from_bytes(buf,size);
	ASSERT_STRING_EQUAL(hex, expected_res);
	lwfree(hex);
}

static void do_test_s64_varint(int64_t nr,int expected_size, char* expected_res)
{
	uint8_t buf[16];
	int size;
	char *hex;
	
	size = varint_s64_encode_buf(nr, buf);
	if ( size != expected_size )
	{
		printf("Expected: %d\nObtained: %d\n", expected_size, size);
	}
	CU_ASSERT_EQUAL(size,expected_size);
	
	hex = hexbytes_from_bytes(buf,size);
	ASSERT_STRING_EQUAL(hex, expected_res);	
	lwfree(hex);
}

static void test_varint(void)
{

	do_test_u64_varint(1, 1, "01");
	do_test_u64_varint(300, 2, "AC02");
	do_test_u64_varint(150, 2, "9601");
	do_test_u64_varint(240, 2, "F001");
	do_test_u64_varint(0x4000, 3, "808001");
  /*
                0100:0000 0000:0000 - input (0x4000)
      1000:0000 1000:0000 0000:0001 - output (0x808001)
       000:0000  000:0000  000:0001 - chop
       000:0001  000:0000  000:0000 - swap
         0:0000 0100:0000 0000:0000 - concat = input
   */
	do_test_u64_varint(2147483647, 5, "FFFFFFFF07");
  /*
              0111:1111 1111:1111 1111:1111 1111:1111 - input (0x7FFFFFFF)
    1111:1111 1111:1111 1111:1111 1111:1111 0000:0111 - output(0xFFFFFFFF07)
     111:1111  111:1111  111:1111  111:1111  000:0111 - chop
     000:0111  111:1111  111:1111  111:1111  111:1111 - swap
              0111:1111 1111:1111 1111:1111 1111:1111 - concat = input
                      |         |         |         |
                   2^32      2^16       2^8       2^0
   */
	do_test_s64_varint(1, 1, "02");
	do_test_s64_varint(-1, 1, "01");
	do_test_s64_varint(-2, 1, "03");

	do_test_u32_varint(2147483647, 5, "FFFFFFFF07");
  /*
              0111:1111 1111:1111 1111:1111 1111:1111 - input (7fffffff)
    1111:1111 1111:1111 1111:1111 1111:1111 0000:0111 - output (ffffff07)
     111:1111  111:1111  111:1111  111:1111  000:0111 - chop
     000:0111  111:1111  111:1111  111:1111  111:1111 - swap
              0111:1111 1111:1111 1111:1111 1111:1111 - concat = input
                      |         |         |         |
                   2^32      2^16       2^8       2^0
   */
	do_test_s32_varint(2147483647, 5, "FEFFFFFF0F");
  /*
              0111:1111 1111:1111 1111:1111 1111:1111 - input (7fffffff)
    1111:1110 1111:1111 1111:1111 1111:1111 0000:1111 - output(feffffff0f)
    1111:1111 1111:1111 1111:1111 1111:1111 0000:0111 - zigzag (ffffff07)
     111:1111  111:1111  111:1111  111:1111  000:0111 - chop
     000:0111  111:1111  111:1111  111:1111  111:1111 - swap
              0111:1111 1111:1111 1111:1111 1111:1111 - concat = input
                      |         |         |         |
                   2^32      2^16       2^8       2^0
   */
	do_test_s32_varint(-2147483648, 5, "FFFFFFFF0F");

	do_test_s32_varint(1, 1, "02");
  /*
    0000:0001 - input (01)
    0000:0010 - A: input << 1
    0000:0000 - B: input >> 31
    0000:0010 - zigzag (A xor B) == output
   */

	do_test_s32_varint(-1, 1, "01");
  /*
    1111:1111 ... 1111:1111 - input (FFFFFFFF)
    1111:1111 ... 1111:1110 - A: input << 1
    1111:1111 ... 1111:1111 - B: input >> 31
    0000:0000 ... 0000:0001 - zigzag (A xor B) == output
   */


}


static void do_test_u64_roundtrip(uint64_t i64_in)
{
	uint8_t buffer[16];
	uint64_t i64_out;
	size_t size_in, size_out;
	size_in = varint_u64_encode_buf(i64_in, buffer);
	i64_out = varint_u64_decode(buffer, buffer + size_in, &size_out);
	CU_ASSERT_EQUAL(i64_in, i64_out);
	CU_ASSERT_EQUAL(size_in, size_out);
}

static void do_test_s64_roundtrip(int64_t i64_in)
{
	uint8_t buffer[16];
	int64_t i64_out;
	size_t size_in, size_out;
	size_in = varint_s64_encode_buf(i64_in, buffer);
	i64_out = varint_s64_decode(buffer, buffer + size_in, &size_out);
	CU_ASSERT_EQUAL(i64_in, i64_out);
	CU_ASSERT_EQUAL(size_in, size_out);
}

static void test_varint_roundtrip(void)
{
	int i;
	for ( i = 0; i < 1024; i += 63 )
	{
		do_test_u64_roundtrip(i);
		do_test_s64_roundtrip(i);
		do_test_s64_roundtrip(-1*i);
	}
}

static void test_zigzag(void)
{
	int64_t a;
	int32_t b;
	int i;

	for ( i = 1; i < 1024; i += 31 )
	{
		a = b = i;
		CU_ASSERT_EQUAL(a, unzigzag64(zigzag64(a)));
		CU_ASSERT_EQUAL(b, unzigzag32(zigzag64(b)));
		
		a = b = -1 * i;
		CU_ASSERT_EQUAL(a, unzigzag64(zigzag64(a)));
		CU_ASSERT_EQUAL(b, unzigzag32(zigzag64(b)));
	}

}


/*
** Used by the test harness to register the tests in this file.
*/
void varint_suite_setup(void);
void varint_suite_setup(void)
{
	CU_pSuite suite = CU_add_suite("varint", NULL, NULL);
	PG_ADD_TEST(suite, test_zigzag);
	PG_ADD_TEST(suite, test_varint);
	PG_ADD_TEST(suite, test_varint_roundtrip);
}