-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.h
52 lines (39 loc) · 1.5 KB
/
test.h
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
// Copyright 2015 Red Blob Games <[email protected]>
// https://github.com/redblobgames/cpp-traverse
// License: Apache v2.0 <http://www.apache.org/licenses/LICENSE-2.0.html>
#ifndef TEST_H
#define TEST_H
#include "traverse.h"
#include <iostream>
// Helper function for the unit tests. It's verbose.
template <typename A, typename B>
void _test_equal(const A& a, const B& b, const char* file, int line, bool quiet) {
if (a != b) {
std::cerr << " * FAIL " << file << ':' << std::dec << line << ": (" << std::hex << a << ") != (" << std::hex << b << ")" << std::endl;
} else if (!quiet) {
std::cout << " PASS " << file << ':' << std::dec << line << ": (" << std::hex << a << ")" << std::endl;
}
}
#define TEST_EQ(a, b) _test_equal(a, b, __FILE__, __LINE__, false)
#define TEST_EQ_QUIET(a, b) _test_equal(a, b, __FILE__, __LINE__, true)
// Define some data types used in the test modules
// Basic structures, numbers
struct Point {
int x, y;
};
TRAVERSE_STRUCT(Point, FIELD(x) FIELD(y))
// C and C++ enums, including char to test the signed/unsigned encoding
enum Color { RED, BLUE };
enum class Mood : unsigned int { HAPPY, SAD, HULK_SMASH };
enum class Signed : int { NEGATIVE = -1, ZERO, ONE };
enum class Charred : char { START, END };
// Nested structure with enums, strings, vectors
struct Polygon {
Color color;
Mood mood;
Charred charred;
std::string name;
std::vector<Point> points;
};
TRAVERSE_STRUCT(Polygon, FIELD(color) FIELD(mood) FIELD(charred) FIELD(name) FIELD(points))
#endif