t_abs.c revision 1.1.2.2 1 /* $NetBSD: t_abs.c,v 1.1.2.2 2012/04/17 00:09:12 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jukka Ruohonen.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_abs.c,v 1.1.2.2 2012/04/17 00:09:12 yamt Exp $");
33
34 #include <atf-c.h>
35 #include <inttypes.h>
36 #include <limits.h>
37 #include <stdlib.h>
38
39 struct test {
40 int64_t val;
41 int64_t res;
42 };
43
44 ATF_TC(abs_basic);
45 ATF_TC_HEAD(abs_basic, tc)
46 {
47 atf_tc_set_md_var(tc, "descr", "Test that abs(3) works");
48 }
49
50 ATF_TC_BODY(abs_basic, tc)
51 {
52 static const struct test table[] = {
53 { 0, 0 },
54 { +0, 0 },
55 { -0, 0 },
56 { -0x1010, 0x1010 },
57 { INT_MAX, INT_MAX },
58 { -INT_MAX, INT_MAX },
59 };
60
61 for (size_t i = 0; i < __arraycount(table); i++)
62 ATF_CHECK(abs(table[i].val) == (int)table[i].res);
63 }
64
65 ATF_TC(imaxabs_basic);
66 ATF_TC_HEAD(imaxabs_basic, tc)
67 {
68 atf_tc_set_md_var(tc, "descr", "Test that imaxabs(3) works");
69 }
70
71 ATF_TC_BODY(imaxabs_basic, tc)
72 {
73 static const struct test table[] = {
74 { 0, 0 },
75 { INT_MAX, INT_MAX },
76 { -INT_MAX, INT_MAX },
77 };
78
79 for (size_t i = 0; i < __arraycount(table); i++)
80 ATF_CHECK(imaxabs(table[i].val) == (intmax_t)table[i].res);
81 }
82
83 ATF_TC(labs_basic);
84 ATF_TC_HEAD(labs_basic, tc)
85 {
86 atf_tc_set_md_var(tc, "descr", "Test that labs(3) works");
87 }
88
89 ATF_TC_BODY(labs_basic, tc)
90 {
91 static const struct test table[] = {
92 { 0, 0 },
93 { +0, 0 },
94 { -0, 0 },
95 { -1, 1 },
96 { LONG_MAX, LONG_MAX },
97 { -LONG_MAX, LONG_MAX },
98 { -0x100000000, 0x100000000 },
99 };
100
101 for (size_t i = 0; i < __arraycount(table); i++)
102 ATF_CHECK(labs(table[i].val) == (long int)table[i].res);
103 }
104
105 ATF_TC(llabs_basic);
106 ATF_TC_HEAD(llabs_basic, tc)
107 {
108 atf_tc_set_md_var(tc, "descr", "Test that llabs(3) works");
109 }
110
111 ATF_TC_BODY(llabs_basic, tc)
112 {
113 static const struct test table[] = {
114 { 0, 0 },
115 { +0, 0 },
116 { -0, 0 },
117 { -1, 1 },
118 { LLONG_MAX, LLONG_MAX },
119 { -LLONG_MAX, LLONG_MAX },
120 { -0x100000000, 0x100000000 },
121 };
122
123 for (size_t i = 0; i < __arraycount(table); i++)
124 ATF_CHECK(llabs(table[i].val) == (long long int)table[i].res);
125 }
126
127 ATF_TP_ADD_TCS(tp)
128 {
129
130 ATF_TP_ADD_TC(tp, abs_basic);
131 ATF_TP_ADD_TC(tp, imaxabs_basic);
132 ATF_TP_ADD_TC(tp, labs_basic);
133 ATF_TP_ADD_TC(tp, llabs_basic);
134
135 return atf_no_error();
136 }
137