Gyoji Compiler
Loading...
Searching...
No Matches
test.hpp
1/* Copyright 2025 Jonathan S. Arney
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * https://github.com/jarney/gyoji/blob/master/LICENSE
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#pragma once
16#include <string>
17#include <stdio.h>
18
19#define ASSERT_STR_EQUAL(expect, actual, message) \
20 { \
21 std::string actual_str(actual); \
22 std::string expect_str(expect); \
23 if (expect_str != actual_str) { \
24 fprintf(stderr, \
25 "Assertion failed at %s:%d: %s\n", \
26 __FILE__, __LINE__, message); \
27 fprintf(stderr, "Expected : %s\n", expect_str.c_str()); \
28 fprintf(stderr, "Actual : %s\n", actual_str.c_str()); \
29 exit(1); \
30 } \
31 }
32
33#define ASSERT(expect, actual, message) ASSERT_STR_EQUAL(expect, actual, message)
34
35#define ASSERT_TRUE(actual, message) \
36 { \
37 if (!(actual)) { \
38 fprintf(stderr, \
39 "Assertion failed at %s:%d : %s\n", \
40 __FILE__, __LINE__, message); \
41 fprintf(stderr, "Expected this condition to be true\n"); \
42 exit(1); \
43 } \
44 }
45
46#define ASSERT_FALSE(actual, message) \
47 { \
48 if (actual) { \
49 fprintf(stderr, \
50 "Assertion failed at %s:%d : %s\n", \
51 __FILE__, __LINE__, message); \
52 fprintf(stderr, "Expected this condition to be false\n"); \
53 exit(1); \
54 } \
55 }
56
57#define ASSERT_INT_EQUAL(expect, actual, message) \
58 { \
59 size_t actual_v = (size_t)(actual); \
60 size_t expect_v = (size_t)(expect); \
61 if (expect_v != actual_v) { \
62 fprintf(stderr, \
63 "Assertion failed at %s:%d : %s\n", \
64 __FILE__, __LINE__, message); \
65 fprintf(stderr, "Expected : %ld\n", expect_v); \
66 fprintf(stderr, "Actual : %ld\n", actual_v); \
67 exit(1); \
68 } \
69 }
70
71#define ASSERT_NOT_NULL(actual, message) \
72 { \
73 if (actual.get() == nullptr) { \
74 fprintf(stderr, "Assertion failedat %s:%d %s\n", \
75 __FILE__, __LINE__, message); \
76 exit(1); \
77 } \
78 }
79
80#define ASSERT_NULL(actual, message) \
81 { \
82 if (actual.get() != nullptr) { \
83 fprintf(stderr, "Assertion failedat %s:%d %s\n", \
84 __FILE__, __LINE__, message); \
85 exit(1); \
86 } \
87 }
88
89
90#define ASSERT_BOOL(expect, actual, message) \
91 { \
92 bool expect_val = expect; \
93 bool actual_val = actual; \
94 if (expect_val != actual_val) { \
95 fprintf(stderr, "Assertion failed: %s %d\n", message, __LINE__); \
96 fprintf(stderr, "Expected : %s\n", expect_val ? "true" : "false"); \
97 fprintf(stderr, "Actual : %s\n", actual_val ? "true" : "false"); \
98 exit(1); \
99 } \
100 }
101