xref: /aosp_15_r20/external/llvm-libc/test/IntegrationTest/test.h (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Simple checkers for integrations tests ------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_LIBC_UTILS_INTEGRATION_TEST_TEST_H
10 #define LLVM_LIBC_UTILS_INTEGRATION_TEST_TEST_H
11 
12 #include "src/__support/OSUtil/exit.h"
13 #include "src/__support/OSUtil/io.h"
14 
15 #define __AS_STRING(val) #val
16 #define __CHECK_TRUE(file, line, val, should_exit)                             \
17   if (!(val)) {                                                                \
18     LIBC_NAMESPACE::write_to_stderr(file ":" __AS_STRING(                      \
19         line) ": Expected '" #val "' to be true, but is false\n");             \
20     if (should_exit)                                                           \
21       LIBC_NAMESPACE::internal::exit(127);                                     \
22   }
23 
24 #define __CHECK_FALSE(file, line, val, should_exit)                            \
25   if (val) {                                                                   \
26     LIBC_NAMESPACE::write_to_stderr(file ":" __AS_STRING(                      \
27         line) ": Expected '" #val "' to be false, but is true\n");             \
28     if (should_exit)                                                           \
29       LIBC_NAMESPACE::internal::exit(127);                                     \
30   }
31 
32 #define __CHECK_EQ(file, line, val1, val2, should_exit)                        \
33   if ((val1) != (val2)) {                                                      \
34     LIBC_NAMESPACE::write_to_stderr(file ":" __AS_STRING(                      \
35         line) ": Expected '" #val1 "' to be equal to '" #val2 "'\n");          \
36     if (should_exit)                                                           \
37       LIBC_NAMESPACE::internal::exit(127);                                     \
38   }
39 
40 #define __CHECK_NE(file, line, val1, val2, should_exit)                        \
41   if ((val1) == (val2)) {                                                      \
42     LIBC_NAMESPACE::write_to_stderr(file ":" __AS_STRING(                      \
43         line) ": Expected '" #val1 "' to not be equal to '" #val2 "'\n");      \
44     if (should_exit)                                                           \
45       LIBC_NAMESPACE::internal::exit(127);                                     \
46   }
47 
48 ////////////////////////////////////////////////////////////////////////////////
49 // Boolean checks are handled as comparison to the true / false values.
50 
51 #define EXPECT_TRUE(val) __CHECK_TRUE(__FILE__, __LINE__, val, false)
52 #define ASSERT_TRUE(val) __CHECK_TRUE(__FILE__, __LINE__, val, true)
53 #define EXPECT_FALSE(val) __CHECK_FALSE(__FILE__, __LINE__, val, false)
54 #define ASSERT_FALSE(val) __CHECK_FALSE(__FILE__, __LINE__, val, true)
55 
56 ////////////////////////////////////////////////////////////////////////////////
57 // Binary equality / inequality.
58 
59 #define EXPECT_EQ(val1, val2)                                                  \
60   __CHECK_EQ(__FILE__, __LINE__, (val1), (val2), false)
61 #define ASSERT_EQ(val1, val2)                                                  \
62   __CHECK_EQ(__FILE__, __LINE__, (val1), (val2), true)
63 #define EXPECT_NE(val1, val2)                                                  \
64   __CHECK_NE(__FILE__, __LINE__, (val1), (val2), false)
65 #define ASSERT_NE(val1, val2)                                                  \
66   __CHECK_NE(__FILE__, __LINE__, (val1), (val2), true)
67 
68 ////////////////////////////////////////////////////////////////////////////////
69 // Errno checks.
70 
71 #define ASSERT_ERRNO_EQ(VAL)                                                   \
72   ASSERT_EQ(VAL, static_cast<int>(LIBC_NAMESPACE::libc_errno))
73 #define ASSERT_ERRNO_SUCCESS()                                                 \
74   ASSERT_EQ(0, static_cast<int>(LIBC_NAMESPACE::libc_errno))
75 #define ASSERT_ERRNO_FAILURE()                                                 \
76   ASSERT_NE(0, static_cast<int>(LIBC_NAMESPACE::libc_errno))
77 
78 // Integration tests are compiled with -ffreestanding which stops treating
79 // the main function as a non-overloadable special function. Hence, we use a
80 // convenience macro which declares it 'extern "C"'.
81 //
82 // When we are able to reuse the unit test infrastructure for integration
83 // tests, then we should not need to explicitly declare/define the main
84 // function in individual integration tests. We will not need this macro
85 // then.
86 #define TEST_MAIN extern "C" int main
87 
88 #endif // LLVM_LIBC_UTILS_INTEGRATION_TEST_TEST_H
89