xref: /freebsd/lib/libutil/tests/expand_number_test.c (revision 08300d849485bd6231d3a18cb430cab4279edef3)
1*08300d84SXin LI /*-
2*08300d84SXin LI  * SPDX-License-Identifier: BSD-2-Clause
3*08300d84SXin LI  *
4*08300d84SXin LI  * Copyright (c) 2023 Google LLC
5*08300d84SXin LI  *
6*08300d84SXin LI  * Redistribution and use in source and binary forms, with or without
7*08300d84SXin LI  * modification, are permitted provided that the following conditions
8*08300d84SXin LI  * are met:
9*08300d84SXin LI  * 1. Redistributions of source code must retain the above copyright
10*08300d84SXin LI  *    notice, this list of conditions and the following disclaimer.
11*08300d84SXin LI  * 2. Redistributions in binary form must reproduce the above copyright
12*08300d84SXin LI  *    notice, this list of conditions and the following disclaimer in the
13*08300d84SXin LI  *    documentation and/or other materials provided with the distribution.
14*08300d84SXin LI  *
15*08300d84SXin LI  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*08300d84SXin LI  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*08300d84SXin LI  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*08300d84SXin LI  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*08300d84SXin LI  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*08300d84SXin LI  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*08300d84SXin LI  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*08300d84SXin LI  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*08300d84SXin LI  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*08300d84SXin LI  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*08300d84SXin LI  * SUCH DAMAGE.
26*08300d84SXin LI  */
27*08300d84SXin LI 
28*08300d84SXin LI #include <sys/cdefs.h>
29*08300d84SXin LI __FBSDID("$FreeBSD$");
30*08300d84SXin LI 
31*08300d84SXin LI #include <errno.h>
32*08300d84SXin LI #include <libutil.h>
33*08300d84SXin LI 
34*08300d84SXin LI #include <atf-c.h>
35*08300d84SXin LI 
36*08300d84SXin LI ATF_TC_WITHOUT_HEAD(positivetests);
37*08300d84SXin LI ATF_TC_BODY(positivetests, tc)
38*08300d84SXin LI {
39*08300d84SXin LI 	int retval;
40*08300d84SXin LI 	uint64_t num;
41*08300d84SXin LI 
42*08300d84SXin LI #define positive_tc(string, value) 							\
43*08300d84SXin LI 	do {										\
44*08300d84SXin LI 		ATF_CHECK_ERRNO(0, (retval = expand_number((string), &num)) == 0);	\
45*08300d84SXin LI 		ATF_CHECK_EQ(retval, 0);						\
46*08300d84SXin LI 		ATF_CHECK_EQ(num, (value));						\
47*08300d84SXin LI 	} while (0)
48*08300d84SXin LI 
49*08300d84SXin LI 	positive_tc("123456", 123456);
50*08300d84SXin LI 	positive_tc("123456b", 123456);
51*08300d84SXin LI 	positive_tc("1k", 1024);
52*08300d84SXin LI 	positive_tc("1kb", 1024);
53*08300d84SXin LI 	positive_tc("1K", 1024);
54*08300d84SXin LI 	positive_tc("1KB", 1024);
55*08300d84SXin LI 	positive_tc("1m", 1048576);
56*08300d84SXin LI 	positive_tc("1M", 1048576);
57*08300d84SXin LI 	positive_tc("1g", 1073741824);
58*08300d84SXin LI 	positive_tc("1G", 1073741824);
59*08300d84SXin LI 	positive_tc("1t", 1099511627776);
60*08300d84SXin LI 	positive_tc("1T", 1099511627776);
61*08300d84SXin LI 	positive_tc("1p", 1125899906842624);
62*08300d84SXin LI 	positive_tc("1P", 1125899906842624);
63*08300d84SXin LI 	positive_tc("1e", 1152921504606846976);
64*08300d84SXin LI 	positive_tc("1E", 1152921504606846976);
65*08300d84SXin LI 	positive_tc("15E", 17293822569102704640ULL);
66*08300d84SXin LI }
67*08300d84SXin LI 
68*08300d84SXin LI ATF_TC_WITHOUT_HEAD(negativetests);
69*08300d84SXin LI ATF_TC_BODY(negativetests, tc)
70*08300d84SXin LI {
71*08300d84SXin LI 	int retval;
72*08300d84SXin LI 	uint64_t num;
73*08300d84SXin LI 
74*08300d84SXin LI 	ATF_CHECK_ERRNO(EINVAL, retval = expand_number("", &num));
75*08300d84SXin LI 	ATF_CHECK_ERRNO(EINVAL, retval = expand_number("x", &num));
76*08300d84SXin LI 	ATF_CHECK_ERRNO(EINVAL, retval = expand_number("1bb", &num));
77*08300d84SXin LI 	ATF_CHECK_ERRNO(EINVAL, retval = expand_number("1x", &num));
78*08300d84SXin LI 	ATF_CHECK_ERRNO(EINVAL, retval = expand_number("1kx", &num));
79*08300d84SXin LI 	ATF_CHECK_ERRNO(ERANGE, retval = expand_number("16E", &num));
80*08300d84SXin LI }
81*08300d84SXin LI 
82*08300d84SXin LI ATF_TP_ADD_TCS(tp)
83*08300d84SXin LI {
84*08300d84SXin LI 	ATF_TP_ADD_TC(tp, positivetests);
85*08300d84SXin LI 	ATF_TP_ADD_TC(tp, negativetests);
86*08300d84SXin LI 	return (atf_no_error());
87*08300d84SXin LI }
88