t_atoi.c revision 1.1 1 1.1 jruoho /* $NetBSD: t_atoi.c,v 1.1 2012/03/29 05:42:31 jruoho Exp $ */
2 1.1 jruoho
3 1.1 jruoho /*-
4 1.1 jruoho * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 1.1 jruoho * All rights reserved.
6 1.1 jruoho *
7 1.1 jruoho * This code is derived from software contributed to The NetBSD Foundation
8 1.1 jruoho * by Jukka Ruohonen.
9 1.1 jruoho *
10 1.1 jruoho * Redistribution and use in source and binary forms, with or without
11 1.1 jruoho * modification, are permitted provided that the following conditions
12 1.1 jruoho * are met:
13 1.1 jruoho * 1. Redistributions of source code must retain the above copyright
14 1.1 jruoho * notice, this list of conditions and the following disclaimer.
15 1.1 jruoho * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 jruoho * notice, this list of conditions and the following disclaimer in the
17 1.1 jruoho * documentation and/or other materials provided with the distribution.
18 1.1 jruoho *
19 1.1 jruoho * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 jruoho * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 jruoho * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 jruoho * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 jruoho * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 jruoho * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 jruoho * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 jruoho * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 jruoho * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 jruoho * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 jruoho * POSSIBILITY OF SUCH DAMAGE.
30 1.1 jruoho */
31 1.1 jruoho #include <sys/cdefs.h>
32 1.1 jruoho __RCSID("$NetBSD: t_atoi.c,v 1.1 2012/03/29 05:42:31 jruoho Exp $");
33 1.1 jruoho
34 1.1 jruoho #include <atf-c.h>
35 1.1 jruoho #include <float.h>
36 1.1 jruoho #include <limits.h>
37 1.1 jruoho #include <stdio.h>
38 1.1 jruoho #include <stdlib.h>
39 1.1 jruoho
40 1.1 jruoho ATF_TC(atof_strtod);
41 1.1 jruoho ATF_TC_HEAD(atof_strtod, tc)
42 1.1 jruoho {
43 1.1 jruoho atf_tc_set_md_var(tc, "descr",
44 1.1 jruoho "Test that atof(3) matches the corresponding strtod(3) call");
45 1.1 jruoho }
46 1.1 jruoho
47 1.1 jruoho ATF_TC_BODY(atof_strtod, tc)
48 1.1 jruoho {
49 1.1 jruoho char buf[64];
50 1.1 jruoho
51 1.1 jruoho (void)snprintf(buf, sizeof(buf), "%f\n", DBL_MAX);
52 1.1 jruoho
53 1.1 jruoho ATF_REQUIRE(atof("0") == strtod("0", NULL));
54 1.1 jruoho ATF_REQUIRE(atof("-1") == strtod("-1", NULL));
55 1.1 jruoho ATF_REQUIRE(atof(buf) == strtod(buf, NULL));
56 1.1 jruoho }
57 1.1 jruoho
58 1.1 jruoho ATF_TC(atoi_strtol);
59 1.1 jruoho ATF_TC_HEAD(atoi_strtol, tc)
60 1.1 jruoho {
61 1.1 jruoho atf_tc_set_md_var(tc, "descr",
62 1.1 jruoho "Test that atoi(3) matches the corresponding strtol(3) call");
63 1.1 jruoho }
64 1.1 jruoho
65 1.1 jruoho ATF_TC_BODY(atoi_strtol, tc)
66 1.1 jruoho {
67 1.1 jruoho char buf[64];
68 1.1 jruoho
69 1.1 jruoho (void)snprintf(buf, sizeof(buf), "%d\n", INT_MAX);
70 1.1 jruoho
71 1.1 jruoho ATF_REQUIRE(atoi("0") == strtol("0", NULL, 10));
72 1.1 jruoho ATF_REQUIRE(atoi("-1") == strtol("-1", NULL, 10));
73 1.1 jruoho ATF_REQUIRE(atoi(buf) == strtol(buf, NULL, 10));
74 1.1 jruoho }
75 1.1 jruoho
76 1.1 jruoho ATF_TC(atol_strtol);
77 1.1 jruoho ATF_TC_HEAD(atol_strtol, tc)
78 1.1 jruoho {
79 1.1 jruoho atf_tc_set_md_var(tc, "descr",
80 1.1 jruoho "Test that atol(3) matches the corresponding strtol(3) call");
81 1.1 jruoho }
82 1.1 jruoho
83 1.1 jruoho ATF_TC_BODY(atol_strtol, tc)
84 1.1 jruoho {
85 1.1 jruoho char buf[64];
86 1.1 jruoho
87 1.1 jruoho (void)snprintf(buf, sizeof(buf), "%ld\n", LONG_MAX);
88 1.1 jruoho
89 1.1 jruoho ATF_REQUIRE(atol("0") == strtol("0", NULL, 10));
90 1.1 jruoho ATF_REQUIRE(atol("-1") == strtol("-1", NULL, 10));
91 1.1 jruoho ATF_REQUIRE(atol(buf) == strtol(buf, NULL, 10));
92 1.1 jruoho }
93 1.1 jruoho
94 1.1 jruoho ATF_TP_ADD_TCS(tp)
95 1.1 jruoho {
96 1.1 jruoho
97 1.1 jruoho ATF_TP_ADD_TC(tp, atof_strtod);
98 1.1 jruoho ATF_TP_ADD_TC(tp, atoi_strtol);
99 1.1 jruoho ATF_TP_ADD_TC(tp, atol_strtol);
100 1.1 jruoho
101 1.1 jruoho return atf_no_error();
102 1.1 jruoho }
103