source: mainline/uspace/lib/c/test/imath.c@ d5a89a3

Last change on this file since d5a89a3 was e3272101, checked in by Matthieu Riolo <matthieu.riolo@…>, 7 years ago

make ilog10_u64() return an errno_t

The function ilog10_u64() used to return 0
for the value 0. Which is not correct. Either
NaN or -Infinity are correct, but not 0, since
it would be ambiguous with log(1). To ensure this
case the function ilog10_u64() has been changed
to return a errno_t indicating a failure.

  • Property mode set to 100644
File size: 2.7 KB
Line 
1/*
2 * Copyright (c) 2019 Matthieu Riolo
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <pcut/pcut.h>
30#include <imath.h>
31
32static uint64_t MAX_NUM = 10000000000000000000U;
33static unsigned MAX_EXP = 19;
34
35PCUT_INIT;
36
37PCUT_TEST_SUITE(imath);
38
39PCUT_TEST(ipow10_u64_zero)
40{
41 errno_t ret;
42 uint64_t result;
43 ret = ipow10_u64(0, &result);
44
45 PCUT_ASSERT_ERRNO_VAL(EOK, ret);
46 PCUT_ASSERT_INT_EQUALS(1, result);
47}
48
49PCUT_TEST(ipow10_u64_one)
50{
51 errno_t ret;
52 uint64_t result;
53 ret = ipow10_u64(1, &result);
54
55 PCUT_ASSERT_ERRNO_VAL(EOK, ret);
56 PCUT_ASSERT_INT_EQUALS(10, result);
57}
58
59PCUT_TEST(ipow10_u64_max)
60{
61 errno_t ret;
62 uint64_t result;
63 ret = ipow10_u64(MAX_EXP, &result);
64
65 PCUT_ASSERT_ERRNO_VAL(EOK, ret);
66 PCUT_ASSERT_INT_EQUALS(MAX_NUM, result);
67}
68
69PCUT_TEST(ipow10_u64_too_large)
70{
71 errno_t ret;
72 uint64_t result;
73 ret = ipow10_u64(MAX_EXP + 1, &result);
74
75 PCUT_ASSERT_ERRNO_VAL(ERANGE, ret);
76}
77
78PCUT_TEST(ilog10_u64_zero)
79{
80 unsigned res;
81 errno_t ret = ilog10_u64(0, &res);
82 PCUT_ASSERT_ERRNO_VAL(ERANGE, ret);
83}
84
85PCUT_TEST(ilog10_u64_one)
86{
87 unsigned res;
88 errno_t ret = ilog10_u64(1, &res);
89 PCUT_ASSERT_ERRNO_VAL(EOK, ret);
90 PCUT_ASSERT_INT_EQUALS(0, res);
91}
92
93PCUT_TEST(ilog10_u64_max)
94{
95 unsigned res;
96 errno_t ret = ilog10_u64(MAX_NUM, &res);
97 PCUT_ASSERT_ERRNO_VAL(EOK, ret);
98 PCUT_ASSERT_INT_EQUALS(MAX_EXP, res);
99}
100
101PCUT_EXPORT(imath);
Note: See TracBrowser for help on using the repository browser.