summaryrefslogtreecommitdiff
path: root/examples/utest/testcases/posix/stdlib_h/functions/atol_tc.c
blob: a2a954d3a854c55a4d04d1f1317dbea9af43728d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>



struct atol_data
{
    char string[15];    // long max 2147483647
    int  ret_num;
};

struct atol_data test_data1[] =
{
    /* positive integer */
    {"0", 0},
    {"1", 1},
    {"1.123", 1},
    {"123", 123},
    {"98993489", 98993489},
    {"98993489.12", 98993489},
    {"2147483647", 2147483647},

    /* negtive integer */
    {"-1", -1},
    {"-1.123", -1},
    {"-123", -123},
    {"-98993489", -98993489},
    {"-98993489.12", -98993489},
    {"-2147483647", -2147483647},

    /* letters and numbers */
    {"12a45", 12},
    {"-12a45", -12},
    {"12/45", 12},
    {"-12/45", -12},

    /* cannot be resolved */
    {"", 0},
    {" ", 0},
    {"abc12", 0},
    {" abc12", 0},
    /* {NULL, -1}  compiler warning */
};

#include <utest.h>
int atol_entry(void)
{
    int i = 0;
    int res = 0;
    for (i = 0; i < sizeof(test_data1[0]); i++)
    {
        res = atol(test_data1[i].string);
        uassert_int_equal(res, test_data1[i].ret_num);
    }
    return 0;
}

static void test_atol(void)
{
    atol_entry();
}
static void testcase(void)
{
    UTEST_UNIT_RUN(test_atol);
}
UTEST_TC_EXPORT(testcase, "posix.stdlib_h.atol_tc.c", RT_NULL, RT_NULL, 10);