Changeset a4e78743 in mainline


Ignore:
Timestamp:
2019-02-26T16:10:39Z (6 years ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
951e451
Parents:
5e801dc
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2019-02-26 16:07:08)
git-committer:
Jiří Zárevúcky <zarevucky.jiri@…> (2019-02-26 16:10:39)
Message:

Move string-to-int conversion tests to a separate file, and add a few

Some of the newly added tests detect bugs in current implementation.
Fixes and more tests are WIP.

Location:
uspace/lib
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/Makefile

    r5e801dc ra4e78743  
    212212        test/str.c \
    213213        test/string.c \
     214        test/strtol.c \
    214215        test/uuid.c
    215216
  • uspace/lib/c/test/main.c

    r5e801dc ra4e78743  
    5353PCUT_IMPORT(str);
    5454PCUT_IMPORT(string);
     55PCUT_IMPORT(strtol);
    5556PCUT_IMPORT(table);
    5657PCUT_IMPORT(uuid);
  • uspace/lib/c/test/stdlib.c

    r5e801dc ra4e78743  
    6363}
    6464
    65 /** atoi function */
    66 PCUT_TEST(atoi)
    67 {
    68         int i;
    69 
    70         i = atoi(" \t42");
    71         PCUT_ASSERT_TRUE(i == 42);
    72 }
    73 
    74 /** atol function */
    75 PCUT_TEST(atol)
    76 {
    77         long li;
    78 
    79         li = atol(" \t42");
    80         PCUT_ASSERT_TRUE(li == 42);
    81 }
    82 
    83 /** atoll function */
    84 PCUT_TEST(atoll)
    85 {
    86         long long lli;
    87 
    88         lli = atoll(" \t42");
    89         PCUT_ASSERT_TRUE(lli == 42);
    90 }
    91 
    9265/** strtold function */
    9366#include <stdio.h>
     
    10174        printf("ld=%.10lf\n", (double)ld);
    10275        PCUT_ASSERT_TRUE(ld == 42.0);
    103 }
    104 
    105 /** strtol function */
    106 PCUT_TEST(strtol)
    107 {
    108         long li;
    109         char *ep;
    110 
    111         li = strtol(" \t42x", &ep, 10);
    112         PCUT_ASSERT_TRUE(li == 42);
    113         PCUT_ASSERT_TRUE(*ep == 'x');
    114 }
    115 
    116 /** strtol function with auto-detected base 10 */
    117 PCUT_TEST(strtol_dec_auto)
    118 {
    119         long li;
    120         char *ep;
    121 
    122         li = strtol(" \t42x", &ep, 0);
    123         PCUT_ASSERT_TRUE(li == 42);
    124         PCUT_ASSERT_TRUE(*ep == 'x');
    125 }
    126 
    127 /** strtol function with octal number */
    128 PCUT_TEST(strtol_oct)
    129 {
    130         long li;
    131         char *ep;
    132 
    133         li = strtol(" \t052x", &ep, 8);
    134         PCUT_ASSERT_TRUE(li == 052);
    135         PCUT_ASSERT_TRUE(*ep == 'x');
    136 }
    137 
    138 /** strtol function with octal number with prefix */
    139 #include <stdio.h>
    140 PCUT_TEST(strtol_oct_prefix)
    141 {
    142         long li;
    143         char *ep;
    144 
    145         li = strtol(" \t052x", &ep, 0);
    146         printf("li=%ld (0%lo)\n", li, li);
    147         PCUT_ASSERT_TRUE(li == 052);
    148         PCUT_ASSERT_TRUE(*ep == 'x');
    149 }
    150 
    151 /** strtol function with hex number */
    152 PCUT_TEST(strtol_hex)
    153 {
    154         long li;
    155         char *ep;
    156 
    157         li = strtol(" \t2ax", &ep, 16);
    158         PCUT_ASSERT_TRUE(li == 0x2a);
    159         PCUT_ASSERT_TRUE(*ep == 'x');
    160 }
    161 
    162 /** strtol function with hex number with hex prefix */
    163 PCUT_TEST(strtol_hex_prefixed)
    164 {
    165         long li;
    166         char *ep;
    167 
    168         li = strtol(" \t0x2ax", &ep, 0);
    169         PCUT_ASSERT_TRUE(li == 0x2a);
    170         PCUT_ASSERT_TRUE(*ep == 'x');
    171 }
    172 
    173 /** strtol function with base 16 and number with 0x prefix */
    174 PCUT_TEST(strtol_base16_prefix)
    175 {
    176         long li;
    177         char *ep;
    178 
    179         li = strtol(" \t0x1y", &ep, 16);
    180         printf("li=%ld\n", li);
    181         PCUT_ASSERT_TRUE(li == 1);
    182         PCUT_ASSERT_TRUE(*ep == 'y');
    183 }
    184 
    185 /** strtol function with base 36 number */
    186 PCUT_TEST(strtol_base36)
    187 {
    188         long li;
    189         char *ep;
    190 
    191         li = strtol(" \tz1.", &ep, 36);
    192         PCUT_ASSERT_TRUE(li == 35 * 36 + 1);
    193         PCUT_ASSERT_TRUE(*ep == '.');
    19476}
    19577
  • uspace/lib/pcut/include/pcut/asserts.h

    r5e801dc ra4e78743  
    192192                                pcut_expected_eval, pcut_actual_eval, \
    193193                                #expected, #actual); \
     194                } \
     195        } while (0)
     196
     197/** Assertion for checking that two integers are equal.
     198 *
     199 * @param expected Expected (correct) value.
     200 * @param actual Actually obtained (computed) value we wish to test.
     201 */
     202#define PCUT_ASSERT_UINT_EQUALS(expected, actual) \
     203        do {\
     204                unsigned long long pcut_expected_eval = (expected); \
     205                unsigned long long pcut_actual_eval = (actual); \
     206                if (pcut_expected_eval != pcut_actual_eval) { \
     207                        PCUT_ASSERTION_FAILED("Expected <%llu> but got <%llu> (%s != %s)", \
     208                                pcut_expected_eval, pcut_actual_eval, \
     209                                #expected, #actual); \
     210                } \
     211        } while (0)
     212
     213/** Assertion for checking that two pointers are equal.
     214 *
     215 * @param expected Expected (correct) value.
     216 * @param actual Actually obtained (computed) value we wish to test.
     217 */
     218#define PCUT_ASSERT_PTR_EQUALS(expected, actual) \
     219        do {\
     220                const void *pcut_expected_eval = (expected); \
     221                const void *pcut_actual_eval = (actual); \
     222                if (pcut_expected_eval != pcut_actual_eval) { \
     223                        PCUT_ASSERTION_FAILED("Expected '" #actual "' = '" #expected "' = <%p> but got '" #actual "' = <%p>", \
     224                                pcut_expected_eval, pcut_actual_eval); \
    194225                } \
    195226        } while (0)
Note: See TracChangeset for help on using the changeset viewer.