1 /* vim: set ts=8 sts=4 sw=4 tw=80 noet: */
2 /*======================================================================
3 Copyright (C) 2004,2005,2009 Walter Doekes <walter+tthsum@wjd.nu>
4 This file is part of tthsum.
5 
6 tthsum is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 tthsum is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with tthsum.  If not, see <http://www.gnu.org/licenses/>.
18 ======================================================================*/
19 #include "types.h"
20 
21 #include "test.h"
22 
23 
test_type8()24 static int test_type8() {
25     int8_t i = 127;
26     uint8_t u = 127;
27     i += 1;	TEST_PASS(i == -128, "(s) 127 + 1 != -128");
28     u += 1;	TEST_PASS(u == 128, "127 + 1 != 128");
29     i += 128;	TEST_PASS(i == 0, "(s) -128 + 128 != 0");
30     u += 128;	TEST_PASS(u == 0, "128 + 128 != 0");
31     i -= 1;	TEST_PASS(i == -1, "(s) 0 - 1 != -1");
32     u -= 1;	TEST_PASS(u == 255, "0 - 1 != 255");
33     return 0;
34 }
35 
test_type16()36 static int test_type16() {
37     int16_t i = 32767;
38     uint16_t u = 32767;
39     i += 1;	TEST_PASS(i == -32768, "(s) 32767 + 1 != -32768");
40     u += 1;	TEST_PASS(u == 32768, "32767 + 1 != 32768");
41     i += 32768;	TEST_PASS(i == 0, "(s) -32768 + 32768 != 0");
42     u += 32768;	TEST_PASS(u == 0, "32768 + 32768 != 0");
43     i -= 1;	TEST_PASS(i == -1, "(s) 0 - 1 != -1");
44     u -= 1;	TEST_PASS(u == 65535, "0 - 1 != 65535");
45     return 0;
46 }
47 
test_type32()48 static int test_type32() {
49     int32_t i = 2147483647;
50     uint32_t u = 2147483647;
51     /* gcc on 32 bits emits a warning about -2147483648 being "unsigned only
52      * in ISO C90", but I don't want it to be unsigned anyway... */
53     i += 1; TEST_PASS(i == (-2147483647 - 1),
54 	    "(s) 2147483647 + 1 != -2147483648");
55     u += 1; TEST_PASS(u == 2147483648U, "2147483647 + 1 != 2147483648");
56     i += 2147483648U; TEST_PASS(i == 0, "(s) -2147483648 + 2147483648 != 0");
57     u += 2147483648U; TEST_PASS(u == 0, "2147483648 + 2147483648 != 0");
58     i -= 1; TEST_PASS(i == -1, "(s) 0 - 1 != -1");
59     u -= 1; TEST_PASS(u == 4294967295U, "0 - 1 != 4294967295");
60     return 0;
61 }
62 
test_type64()63 static int test_type64() {
64     int64_t i = _LL(9223372036854775807);
65     uint64_t u = _ULL(9223372036854775807);
66     /* gcc emits a warning about _LL(-9223372036854775808) being too large,
67      * which is wrong. That value is perfectly fine. */
68     i += 1; TEST_PASS(i == _LL(-9223372036854775807) - _LL(1),
69 	    "(s) 9223372036854775807 + 1 != -9223372036854775808");
70     u += 1; TEST_PASS(u == _ULL(9223372036854775808),
71 	    "9223372036854775807 + 1 != 9223372036854775808");
72     i += _ULL(9223372036854775808); TEST_PASS(i == 0,
73 	    "(s) -9223372036854775808 + 9223372036854775808 != 0");
74     u += _ULL(9223372036854775808); TEST_PASS(u == 0,
75 	    "9223372036854775808 + 9223372036854775808 != 0");
76     i -= 1; TEST_PASS(i == -1,
77 	    "(s) 0 - 1 != -1");
78     u -= 1; TEST_PASS(u == _ULL(18446744073709551615),
79 	    "0 - 1 != 18446744073709551615");
80     return 0;
81 }
82 
83 
84 TESTS(types_test)
85     TEST(test_type8);
86     TEST(test_type16);
87     TEST(test_type32);
88     TEST(test_type64);
89 ENDTESTS
90