1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_abs.c,v 1.3 2014/03/01 22:38:13 joerg Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc * Copyright (c) 2012 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc * All rights reserved.
611be35a1SLionel Sambuc *
711be35a1SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
811be35a1SLionel Sambuc * by Jukka Ruohonen.
911be35a1SLionel Sambuc *
1011be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
1111be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
1211be35a1SLionel Sambuc * are met:
1311be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1411be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1511be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1611be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1711be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
1811be35a1SLionel Sambuc *
1911be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2011be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2111be35a1SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2211be35a1SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2311be35a1SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2411be35a1SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2511be35a1SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2611be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2711be35a1SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2811be35a1SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2911be35a1SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
3011be35a1SLionel Sambuc */
3111be35a1SLionel Sambuc #include <sys/cdefs.h>
32*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: t_abs.c,v 1.3 2014/03/01 22:38:13 joerg Exp $");
3311be35a1SLionel Sambuc
3411be35a1SLionel Sambuc #include <atf-c.h>
3511be35a1SLionel Sambuc #include <inttypes.h>
3611be35a1SLionel Sambuc #include <limits.h>
3711be35a1SLionel Sambuc #include <stdlib.h>
3811be35a1SLionel Sambuc
3911be35a1SLionel Sambuc ATF_TC(abs_basic);
ATF_TC_HEAD(abs_basic,tc)4011be35a1SLionel Sambuc ATF_TC_HEAD(abs_basic, tc)
4111be35a1SLionel Sambuc {
4211be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test that abs(3) works");
4311be35a1SLionel Sambuc }
4411be35a1SLionel Sambuc
ATF_TC_BODY(abs_basic,tc)4511be35a1SLionel Sambuc ATF_TC_BODY(abs_basic, tc)
4611be35a1SLionel Sambuc {
47*0a6a1f1dSLionel Sambuc static const struct {
48*0a6a1f1dSLionel Sambuc int val;
49*0a6a1f1dSLionel Sambuc int res;
50*0a6a1f1dSLionel Sambuc } table[] = {
5111be35a1SLionel Sambuc { 0, 0 },
5211be35a1SLionel Sambuc { +0, 0 },
5311be35a1SLionel Sambuc { -0, 0 },
5411be35a1SLionel Sambuc { -0x1010, 0x1010 },
5511be35a1SLionel Sambuc { INT_MAX, INT_MAX },
5611be35a1SLionel Sambuc { -INT_MAX, INT_MAX },
5711be35a1SLionel Sambuc };
5811be35a1SLionel Sambuc
5911be35a1SLionel Sambuc for (size_t i = 0; i < __arraycount(table); i++)
60*0a6a1f1dSLionel Sambuc ATF_CHECK(abs(table[i].val) == table[i].res);
6111be35a1SLionel Sambuc }
6211be35a1SLionel Sambuc
6311be35a1SLionel Sambuc ATF_TC(imaxabs_basic);
ATF_TC_HEAD(imaxabs_basic,tc)6411be35a1SLionel Sambuc ATF_TC_HEAD(imaxabs_basic, tc)
6511be35a1SLionel Sambuc {
6611be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test that imaxabs(3) works");
6711be35a1SLionel Sambuc }
6811be35a1SLionel Sambuc
ATF_TC_BODY(imaxabs_basic,tc)6911be35a1SLionel Sambuc ATF_TC_BODY(imaxabs_basic, tc)
7011be35a1SLionel Sambuc {
71*0a6a1f1dSLionel Sambuc static const struct {
72*0a6a1f1dSLionel Sambuc intmax_t val;
73*0a6a1f1dSLionel Sambuc intmax_t res;
74*0a6a1f1dSLionel Sambuc } table[] = {
7511be35a1SLionel Sambuc { 0, 0 },
7611be35a1SLionel Sambuc { INT_MAX, INT_MAX },
7711be35a1SLionel Sambuc { -INT_MAX, INT_MAX },
78*0a6a1f1dSLionel Sambuc { LONG_MAX, LONG_MAX },
79*0a6a1f1dSLionel Sambuc { -LONG_MAX, LONG_MAX },
80*0a6a1f1dSLionel Sambuc { LLONG_MAX, LLONG_MAX },
81*0a6a1f1dSLionel Sambuc { -LLONG_MAX, LLONG_MAX },
82*0a6a1f1dSLionel Sambuc { INT_MAX, INT_MAX },
83*0a6a1f1dSLionel Sambuc { -INT_MAX, INT_MAX },
8411be35a1SLionel Sambuc };
8511be35a1SLionel Sambuc
8611be35a1SLionel Sambuc for (size_t i = 0; i < __arraycount(table); i++)
87*0a6a1f1dSLionel Sambuc ATF_CHECK(imaxabs(table[i].val) == table[i].res);
8811be35a1SLionel Sambuc }
8911be35a1SLionel Sambuc
9011be35a1SLionel Sambuc ATF_TC(labs_basic);
ATF_TC_HEAD(labs_basic,tc)9111be35a1SLionel Sambuc ATF_TC_HEAD(labs_basic, tc)
9211be35a1SLionel Sambuc {
9311be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test that labs(3) works");
9411be35a1SLionel Sambuc }
9511be35a1SLionel Sambuc
ATF_TC_BODY(labs_basic,tc)9611be35a1SLionel Sambuc ATF_TC_BODY(labs_basic, tc)
9711be35a1SLionel Sambuc {
98*0a6a1f1dSLionel Sambuc static const struct {
99*0a6a1f1dSLionel Sambuc long val;
100*0a6a1f1dSLionel Sambuc long res;
101*0a6a1f1dSLionel Sambuc } table[] = {
10211be35a1SLionel Sambuc { 0, 0 },
10311be35a1SLionel Sambuc { +0, 0 },
10411be35a1SLionel Sambuc { -0, 0 },
10511be35a1SLionel Sambuc { -1, 1 },
10611be35a1SLionel Sambuc { LONG_MAX, LONG_MAX },
10711be35a1SLionel Sambuc { -LONG_MAX, LONG_MAX },
108*0a6a1f1dSLionel Sambuc { INT_MAX, INT_MAX },
109*0a6a1f1dSLionel Sambuc { -INT_MAX, INT_MAX },
11011be35a1SLionel Sambuc };
11111be35a1SLionel Sambuc
11211be35a1SLionel Sambuc for (size_t i = 0; i < __arraycount(table); i++)
113*0a6a1f1dSLionel Sambuc ATF_CHECK(labs(table[i].val) == table[i].res);
11411be35a1SLionel Sambuc }
11511be35a1SLionel Sambuc
11611be35a1SLionel Sambuc ATF_TC(llabs_basic);
ATF_TC_HEAD(llabs_basic,tc)11711be35a1SLionel Sambuc ATF_TC_HEAD(llabs_basic, tc)
11811be35a1SLionel Sambuc {
11911be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test that llabs(3) works");
12011be35a1SLionel Sambuc }
12111be35a1SLionel Sambuc
ATF_TC_BODY(llabs_basic,tc)12211be35a1SLionel Sambuc ATF_TC_BODY(llabs_basic, tc)
12311be35a1SLionel Sambuc {
124*0a6a1f1dSLionel Sambuc static const struct {
125*0a6a1f1dSLionel Sambuc long long val;
126*0a6a1f1dSLionel Sambuc long long res;
127*0a6a1f1dSLionel Sambuc } table[] = {
12811be35a1SLionel Sambuc { 0, 0 },
12911be35a1SLionel Sambuc { +0, 0 },
13011be35a1SLionel Sambuc { -0, 0 },
13111be35a1SLionel Sambuc { -1, 1 },
132*0a6a1f1dSLionel Sambuc { INT_MAX, INT_MAX },
133*0a6a1f1dSLionel Sambuc { -INT_MAX, INT_MAX },
134*0a6a1f1dSLionel Sambuc { LONG_MAX, LONG_MAX },
135*0a6a1f1dSLionel Sambuc { -LONG_MAX, LONG_MAX },
13611be35a1SLionel Sambuc { LLONG_MAX, LLONG_MAX },
13711be35a1SLionel Sambuc { -LLONG_MAX, LLONG_MAX },
138*0a6a1f1dSLionel Sambuc { -0x100000000LL, 0x100000000LL },
13911be35a1SLionel Sambuc };
14011be35a1SLionel Sambuc
14111be35a1SLionel Sambuc for (size_t i = 0; i < __arraycount(table); i++)
142*0a6a1f1dSLionel Sambuc ATF_CHECK(llabs(table[i].val) == table[i].res);
14311be35a1SLionel Sambuc }
14411be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)14511be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
14611be35a1SLionel Sambuc {
14711be35a1SLionel Sambuc
14811be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, abs_basic);
14911be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, imaxabs_basic);
15011be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, labs_basic);
15111be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, llabs_basic);
15211be35a1SLionel Sambuc
15311be35a1SLionel Sambuc return atf_no_error();
15411be35a1SLionel Sambuc }
155