File: helpers.test.cpp

package info (click to toggle)
tilemaker 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 78,952 kB
  • sloc: cpp: 29,461; ansic: 12,510; makefile: 229; ruby: 77; sh: 43
file content (95 lines) | stat: -rw-r--r-- 2,536 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
#include <iostream>
#include "external/minunit.h"
#include "helpers.h"

MU_TEST(test_get_chunks) {
	{
		auto rv = getNewlineChunks("test/test.jsonl", 1);
		mu_check(rv.size() == 1);
		mu_check(rv[0].offset == 0);
		mu_check(rv[0].length == 24);
	}

	{
		auto rv = getNewlineChunks("test/test.jsonl", 2);
		mu_check(rv.size() == 2);
		mu_check(rv[0].offset == 0);
		mu_check(rv[0].length == 12);
		mu_check(rv[1].offset == 12);
		mu_check(rv[1].length == 12);
	}

	// Dividing into 3 chunks gives a lop-sided result; one of the chunks
	// consists only of whitespace. This is OK.
	{
		auto rv = getNewlineChunks("test/test.jsonl", 3);
		mu_check(rv.size() == 3);
		mu_check(rv[0].offset == 0);
		mu_check(rv[0].length == 12);
		mu_check(rv[1].offset == 12);
		mu_check(rv[1].length == 11);
		mu_check(rv[2].offset == 23);
		mu_check(rv[2].length == 1);
	}

	// Dividing into many more chunks than is possible devolves into
	// one chunk per newline.
	{
		auto rv = getNewlineChunks("test/test.jsonl", 128);
		mu_check(rv.size() == 4);
		mu_check(rv[0].offset == 0);
		mu_check(rv[0].length == 2);
		mu_check(rv[1].offset == 2);
		mu_check(rv[1].length == 10);
		mu_check(rv[2].offset == 12);
		mu_check(rv[2].length == 11);
		mu_check(rv[3].offset == 23);
		mu_check(rv[3].length == 1);
	}
}

MU_TEST(test_compression_gzip) {
	std::string input = "a random string to be compressed";

	for (int level = 1; level < 9; level++) {
		std::string gzipped = compress_string(input, level, true);
		std::string unzipped;
		decompress_string(unzipped, gzipped.data(), gzipped.size(), true);
		mu_check(unzipped == input);
	}

	std::string gzipped = compress_string(input, -1, true);
	std::string unzipped;
	decompress_string(unzipped, gzipped.data(), gzipped.size(), true);
	mu_check(unzipped == input);

}

MU_TEST(test_compression_zlib) {
	std::string input = "a random string to be compressed";

	for (int level = 1; level < 9; level++) {
		std::string gzipped = compress_string(input, level, false);
		std::string unzipped;
		decompress_string(unzipped, gzipped.data(), gzipped.size(), false);
		mu_check(unzipped == input);
	}

	std::string gzipped = compress_string(input, -1, false);
	std::string unzipped;
	decompress_string(unzipped, gzipped.data(), gzipped.size(), false);
	mu_check(unzipped == input);
}


MU_TEST_SUITE(test_suite_helpers) {
	MU_RUN_TEST(test_get_chunks);
	MU_RUN_TEST(test_compression_gzip);
	MU_RUN_TEST(test_compression_zlib);
}

int main() {
	MU_RUN_SUITE(test_suite_helpers);
	MU_REPORT();
	return MU_EXIT_CODE;
}