gitout

A static git page generator
git clone https://git.bracken.jp/gitout.git
Log | Files | Refs | README | LICENSE

test_no_exceptions.cpp (6086B)


      1 // This is free and unencumbered software released into the public domain.
      2 //
      3 // Anyone is free to copy, modify, publish, use, compile, sell, or
      4 // distribute this software, either in source code form or as a compiled
      5 // binary, for any purpose, commercial or non-commercial, and by any
      6 // means.
      7 //
      8 // In jurisdictions that recognize copyright laws, the author or authors
      9 // of this software dedicate any and all copyright interest in the
     10 // software to the public domain. We make this dedication for the benefit
     11 // of the public at large and to the detriment of our heirs and
     12 // successors. We intend this dedication to be an overt act of
     13 // relinquishment in perpetuity of all present and future rights to this
     14 // software under copyright law.
     15 //
     16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
     19 // IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22 // OTHER DEALINGS IN THE SOFTWARE.
     23 //
     24 // For more information, please refer to <http://unlicense.org/>
     25 
     26 #include "utest.h"
     27 
     28 #ifdef _MSC_VER
     29 // disable 'conditional expression is constant' - our examples below use this!
     30 #pragma warning(disable : 4127)
     31 #endif
     32 
     33 UTEST(cpp_no_exceptions, ASSERT_TRUE) { ASSERT_TRUE(1); }
     34 
     35 UTEST(cpp_no_exceptions, ASSERT_FALSE) { ASSERT_FALSE(0); }
     36 
     37 UTEST(cpp_no_exceptions, ASSERT_EQ) { ASSERT_EQ(1, 1); }
     38 
     39 UTEST(cpp_no_exceptions, ASSERT_NE) { ASSERT_NE(1, 2); }
     40 
     41 UTEST(cpp_no_exceptions, ASSERT_LT) { ASSERT_LT(1, 2); }
     42 
     43 UTEST(cpp_no_exceptions, ASSERT_LE) {
     44   ASSERT_LE(1, 1);
     45   ASSERT_LE(1, 2);
     46 }
     47 
     48 UTEST(cpp_no_exceptions, ASSERT_GT) { ASSERT_GT(2, 1); }
     49 
     50 UTEST(cpp_no_exceptions, ASSERT_GE) {
     51   ASSERT_GE(1, 1);
     52   ASSERT_GE(2, 1);
     53 }
     54 
     55 UTEST(cpp_no_exceptions, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); }
     56 
     57 UTEST(cpp_no_exceptions, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); }
     58 
     59 UTEST(cpp_no_exceptions, ASSERT_STRNEQ) {
     60   ASSERT_STRNEQ("foo", "foobar", strlen("foo"));
     61 }
     62 
     63 UTEST(cpp_no_exceptions, ASSERT_STRNNE) {
     64   ASSERT_STRNNE("foo", "barfoo", strlen("foo"));
     65 }
     66 
     67 UTEST(cpp_no_exceptions, EXPECT_TRUE) { EXPECT_TRUE(1); }
     68 
     69 UTEST(cpp_no_exceptions, EXPECT_FALSE) { EXPECT_FALSE(0); }
     70 
     71 UTEST(cpp_no_exceptions, EXPECT_EQ) { EXPECT_EQ(1, 1); }
     72 
     73 UTEST(cpp_no_exceptions, EXPECT_NE) { EXPECT_NE(1, 2); }
     74 
     75 UTEST(cpp_no_exceptions, EXPECT_LT) { EXPECT_LT(1, 2); }
     76 
     77 UTEST(cpp_no_exceptions, EXPECT_LE) {
     78   EXPECT_LE(1, 1);
     79   EXPECT_LE(1, 2);
     80 }
     81 
     82 UTEST(cpp_no_exceptions, EXPECT_GT) { EXPECT_GT(2, 1); }
     83 
     84 UTEST(cpp_no_exceptions, EXPECT_GE) {
     85   EXPECT_GE(1, 1);
     86   EXPECT_GE(2, 1);
     87 }
     88 
     89 UTEST(cpp_no_exceptions, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); }
     90 
     91 UTEST(cpp_no_exceptions, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); }
     92 
     93 UTEST(cpp_no_exceptions, EXPECT_STRNEQ) {
     94   EXPECT_STRNEQ("foo", "foobar", strlen("foo"));
     95 }
     96 
     97 UTEST(cpp_no_exceptions, EXPECT_STRNNE) {
     98   EXPECT_STRNNE("foo", "barfoo", strlen("foo"));
     99 }
    100 
    101 UTEST(cpp_no_exceptions, no_double_eval) {
    102   int i = 0;
    103   ASSERT_EQ(i++, 0);
    104   ASSERT_EQ(i, 1);
    105 }
    106 
    107 struct MyTestF {
    108   int foo;
    109 };
    110 
    111 UTEST_F_SETUP(MyTestF) {
    112   ASSERT_EQ(0, utest_fixture->foo);
    113   utest_fixture->foo = 42;
    114 }
    115 
    116 UTEST_F_TEARDOWN(MyTestF) { ASSERT_EQ(13, utest_fixture->foo); }
    117 
    118 UTEST_F(MyTestF, cpp_no_exceptions_1) {
    119   ASSERT_EQ(42, utest_fixture->foo);
    120   utest_fixture->foo = 13;
    121 }
    122 
    123 UTEST_F(MyTestF, cpp_no_exceptions_2) {
    124   ASSERT_EQ(42, utest_fixture->foo);
    125   utest_fixture->foo = 13;
    126 }
    127 
    128 struct MyTestI {
    129   size_t foo;
    130   size_t bar;
    131 };
    132 
    133 UTEST_I_SETUP(MyTestI) {
    134   ASSERT_EQ(0u, utest_fixture->foo);
    135   ASSERT_EQ(0u, utest_fixture->bar);
    136   utest_fixture->foo = 42;
    137   utest_fixture->bar = utest_index;
    138 }
    139 
    140 UTEST_I_TEARDOWN(MyTestI) {
    141   ASSERT_EQ(13u, utest_fixture->foo);
    142   ASSERT_EQ(utest_index, utest_fixture->bar);
    143 }
    144 
    145 UTEST_I(MyTestI, cpp_no_exceptions_1, 2) {
    146   ASSERT_GT(2u, utest_fixture->bar);
    147   utest_fixture->foo = 13;
    148 }
    149 
    150 UTEST_I(MyTestI, cpp_no_exceptions_2, 128) {
    151   ASSERT_GT(128u, utest_fixture->bar);
    152   utest_fixture->foo = 13;
    153 }
    154 
    155 UTEST(cpp_no_exceptions, Float) {
    156   float a = 1;
    157   float b = 2;
    158   EXPECT_NE(a, b);
    159   ASSERT_NE(a, b);
    160 }
    161 
    162 UTEST(cpp_no_exceptions, Double) {
    163   double a = 1;
    164   double b = 2;
    165   EXPECT_NE(a, b);
    166   ASSERT_NE(a, b);
    167 }
    168 
    169 UTEST(cpp_no_exceptions, LongDouble) {
    170   long double a = 1;
    171   long double b = 2;
    172   EXPECT_NE(a, b);
    173   ASSERT_NE(a, b);
    174 }
    175 
    176 UTEST(cpp_no_exceptions, Char) {
    177   signed char a = 1;
    178   signed char b = 2;
    179   EXPECT_NE(a, b);
    180   ASSERT_NE(a, b);
    181 }
    182 
    183 UTEST(cpp_no_exceptions, UChar) {
    184   unsigned char a = 1;
    185   unsigned char b = 2;
    186   EXPECT_NE(a, b);
    187   ASSERT_NE(a, b);
    188 }
    189 
    190 UTEST(cpp_no_exceptions, Short) {
    191   short a = 1;
    192   short b = 2;
    193   EXPECT_NE(a, b);
    194   ASSERT_NE(a, b);
    195 }
    196 
    197 UTEST(cpp_no_exceptions, UShort) {
    198   unsigned short a = 1;
    199   unsigned short b = 2;
    200   EXPECT_NE(a, b);
    201   ASSERT_NE(a, b);
    202 }
    203 
    204 UTEST(cpp_no_exceptions, Int) {
    205   int a = 1;
    206   int b = 2;
    207   EXPECT_NE(a, b);
    208   ASSERT_NE(a, b);
    209 }
    210 
    211 UTEST(cpp_no_exceptions, UInt) {
    212   unsigned int a = 1;
    213   unsigned int b = 2;
    214   EXPECT_NE(a, b);
    215   ASSERT_NE(a, b);
    216 }
    217 
    218 UTEST(cpp_no_exceptions, Long) {
    219   long a = 1;
    220   long b = 2;
    221   EXPECT_NE(a, b);
    222   ASSERT_NE(a, b);
    223 }
    224 
    225 UTEST(cpp_no_exceptions, ULong) {
    226   unsigned long a = 1;
    227   unsigned long b = 2;
    228   EXPECT_NE(a, b);
    229   ASSERT_NE(a, b);
    230 }
    231 
    232 #if defined(__clang__)
    233 #if __has_warning("-Wunsafe-buffer-usage")
    234 #pragma clang diagnostic push
    235 #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
    236 #endif
    237 #endif
    238 
    239 UTEST(cpp_no_exceptions, Ptr) {
    240   char foo = 42;
    241   EXPECT_NE(&foo, &foo + 1);
    242 }
    243 
    244 static const int data[4] = {42, 13, 6, -53};
    245 
    246 UTEST(cpp_no_exceptions, Array) { EXPECT_NE(data, data + 1); }
    247 
    248 #if defined(__clang__)
    249 #if __has_warning("-Wunsafe-buffer-usage")
    250 #pragma clang diagnostic pop
    251 #endif
    252 #endif
    253 
    254 UTEST(cpp_no_exceptions, Near) {
    255   float a = 42.0f;
    256   float b = 42.01f;
    257   EXPECT_NEAR(a, b, 0.01f);
    258   ASSERT_NEAR(a, b, 0.01f);
    259 }
    260 
    261 UTEST(cpp_no_exceptions, Todo) { UTEST_SKIP("Not yet implemented!"); }