xref: /freebsd/contrib/netbsd-tests/lib/libc/stdlib/t_abs.c (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1 /* $NetBSD: t_abs.c,v 1.3 2014/03/01 22:38:13 joerg 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.3 2014/03/01 22:38:13 joerg Exp $");
33 
34 #include <atf-c.h>
35 #include <inttypes.h>
36 #include <limits.h>
37 #include <stdlib.h>
38 
39 ATF_TC(abs_basic);
40 ATF_TC_HEAD(abs_basic, tc)
41 {
42 	atf_tc_set_md_var(tc, "descr", "Test that abs(3) works");
43 }
44 
45 ATF_TC_BODY(abs_basic, tc)
46 {
47 	static const struct {
48 		int val;
49 		int res;
50 	} table[] = {
51 		{ 0,		0		},
52 		{ +0,		0		},
53 		{ -0,		0		},
54 		{ -0x1010,	0x1010		},
55 		{ INT_MAX,	INT_MAX		},
56 		{ -INT_MAX,	INT_MAX		},
57 	};
58 
59 	for (size_t i = 0; i < __arraycount(table); i++)
60 		ATF_CHECK(abs(table[i].val) == table[i].res);
61 }
62 
63 ATF_TC(imaxabs_basic);
64 ATF_TC_HEAD(imaxabs_basic, tc)
65 {
66 	atf_tc_set_md_var(tc, "descr", "Test that imaxabs(3) works");
67 }
68 
69 ATF_TC_BODY(imaxabs_basic, tc)
70 {
71 	static const struct {
72 		intmax_t val;
73 		intmax_t res;
74 	} table[] = {
75 		{ 0,		0		},
76 		{ INT_MAX,	INT_MAX		},
77 		{ -INT_MAX,	INT_MAX		},
78 		{ LONG_MAX,	LONG_MAX	},
79 		{ -LONG_MAX,	LONG_MAX	},
80 		{ LLONG_MAX,	LLONG_MAX	},
81 		{ -LLONG_MAX,	LLONG_MAX	},
82 		{ INT_MAX,	INT_MAX		},
83 		{ -INT_MAX,	INT_MAX		},
84 	};
85 
86 	for (size_t i = 0; i < __arraycount(table); i++)
87 		ATF_CHECK(imaxabs(table[i].val) == table[i].res);
88 }
89 
90 ATF_TC(labs_basic);
91 ATF_TC_HEAD(labs_basic, tc)
92 {
93 	atf_tc_set_md_var(tc, "descr", "Test that labs(3) works");
94 }
95 
96 ATF_TC_BODY(labs_basic, tc)
97 {
98 	static const struct {
99 		long val;
100 		long res;
101 	} table[] = {
102 		{ 0,		0		},
103 		{ +0,		0		},
104 		{ -0,		0		},
105 		{ -1,		1		},
106 		{ LONG_MAX,	LONG_MAX	},
107 		{ -LONG_MAX,	LONG_MAX	},
108 		{ INT_MAX,	INT_MAX		},
109 		{ -INT_MAX,	INT_MAX		},
110 	};
111 
112 	for (size_t i = 0; i < __arraycount(table); i++)
113 		ATF_CHECK(labs(table[i].val) == table[i].res);
114 }
115 
116 ATF_TC(llabs_basic);
117 ATF_TC_HEAD(llabs_basic, tc)
118 {
119 	atf_tc_set_md_var(tc, "descr", "Test that llabs(3) works");
120 }
121 
122 ATF_TC_BODY(llabs_basic, tc)
123 {
124 	static const struct {
125 		long long val;
126 		long long res;
127 	} table[] = {
128 		{ 0,		0		},
129 		{ +0,		0		},
130 		{ -0,		0		},
131 		{ -1,		1		},
132 		{ INT_MAX,	INT_MAX		},
133 		{ -INT_MAX,	INT_MAX		},
134 		{ LONG_MAX,	LONG_MAX	},
135 		{ -LONG_MAX,	LONG_MAX	},
136 		{ LLONG_MAX,	LLONG_MAX	},
137 		{ -LLONG_MAX,	LLONG_MAX	},
138 		{ -0x100000000LL,	0x100000000LL	},
139 	};
140 
141 	for (size_t i = 0; i < __arraycount(table); i++)
142 		ATF_CHECK(llabs(table[i].val) == table[i].res);
143 }
144 
145 ATF_TP_ADD_TCS(tp)
146 {
147 
148 	ATF_TP_ADD_TC(tp, abs_basic);
149 	ATF_TP_ADD_TC(tp, imaxabs_basic);
150 	ATF_TP_ADD_TC(tp, labs_basic);
151 	ATF_TP_ADD_TC(tp, llabs_basic);
152 
153 	return atf_no_error();
154 }
155