Changeset 6b646dc in mainline for uspace/lib/posix/test/scanf.c


Ignore:
Timestamp:
2014-09-19T12:41:34Z (10 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
6019983
Parents:
bf45993
Message:

Uncomment scanf() tests from libposix

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/posix/test/scanf.c

    rbf45993 r6b646dc  
    3535#include <pcut/pcut.h>
    3636
     37#define EPSILON 0.000001
    3738
    3839PCUT_INIT
     
    6263}
    6364
     65/*
     66 * The following tests were copied from stdio/scanf.c where they were
     67 * commented-out. We ought to convert them to more independent tests
     68 * eventually.
     69 */
     70
     71PCUT_TEST(int_misc) {
     72        unsigned char uhh;
     73        signed char shh;
     74        unsigned short uh;
     75        short sh;
     76        unsigned udef;
     77        int sdef;
     78        unsigned long ul;
     79        long sl;
     80        unsigned long long ull;
     81        long long sll;
     82        void *p;
     83
     84        int rc = posix_sscanf(
     85                "\n j tt % \t -121314 98765 aqw 0765 0x77 0xABCDEF88 -99 884",
     86                " j tt %%%3hhd%1hhu%3hd %3hu%u aqw%n %lo%llx %p %li %lld",
     87                &shh, &uhh, &sh, &uh, &udef, &sdef, &ul, &ull, &p, &sl, &sll);
     88
     89        PCUT_ASSERT_INT_EQUALS(10, rc);
     90
     91        PCUT_ASSERT_INT_EQUALS(-12, shh);
     92        PCUT_ASSERT_INT_EQUALS(1, uhh);
     93        PCUT_ASSERT_INT_EQUALS(314, sh);
     94        PCUT_ASSERT_INT_EQUALS(987, uh);
     95        PCUT_ASSERT_INT_EQUALS(65, udef);
     96        PCUT_ASSERT_INT_EQUALS(28, sdef);
     97        PCUT_ASSERT_INT_EQUALS(0765, ul);
     98        PCUT_ASSERT_INT_EQUALS(0x77, ull);
     99        PCUT_ASSERT_INT_EQUALS(0xABCDEF88, (long long) (uintptr_t) p);
     100        PCUT_ASSERT_INT_EQUALS(-99, sl);
     101        PCUT_ASSERT_INT_EQUALS(884, sll);
     102}
     103
     104PCUT_TEST(double_misc) {
     105        float f;
     106        double d;
     107        long double ld;
     108
     109        int rc = posix_sscanf(
     110                "\n \t\t1.0 -0x555.AP10 1234.5678e12",
     111                "%f %lf %Lf",
     112                &f, &d, &ld);
     113
     114        PCUT_ASSERT_INT_EQUALS(3, rc);
     115
     116        PCUT_ASSERT_DOUBLE_EQUALS(1.0, f, EPSILON);
     117        PCUT_ASSERT_DOUBLE_EQUALS(-0x555.AP10, d, EPSILON);
     118        PCUT_ASSERT_DOUBLE_EQUALS(1234.5678e12, ld, EPSILON);
     119}
     120
     121PCUT_TEST(str_misc) {
     122        char str[20];
     123        char *pstr;
     124
     125        int rc = posix_sscanf(
     126                "\n\n\thello world    \n",
     127                "%5s %ms",
     128                str, &pstr);
     129
     130        PCUT_ASSERT_INT_EQUALS(2, rc);
     131
     132        PCUT_ASSERT_STR_EQUALS("hello", str);
     133        PCUT_ASSERT_STR_EQUALS("world", pstr);
     134
     135        free(pstr);
     136}
     137
     138PCUT_TEST(str_matchers) {
     139        char scanset[20];
     140        char *pscanset;
     141
     142        int rc = posix_sscanf(
     143                "\n\n\th-e-l-l-o world-]    \n",
     144                " %9[-eh-o] %m[^]-]",
     145                scanset, &pscanset);
     146
     147        PCUT_ASSERT_INT_EQUALS(2, rc);
     148
     149        PCUT_ASSERT_STR_EQUALS("h-e-l-l-o", scanset);
     150        PCUT_ASSERT_STR_EQUALS("world", pscanset);
     151
     152        free(pscanset);
     153}
     154
     155PCUT_TEST(char_misc) {
     156        char seq[20];
     157        char *pseq;
     158
     159        int rc = posix_sscanf(
     160                "\n\n\thello world    \n",
     161                " %5c %mc",
     162                seq, &pseq);
     163
     164        PCUT_ASSERT_INT_EQUALS(2, rc);
     165
     166        /* Manually terminate the strings. */
     167        seq[5] = 0;
     168        pseq[1] = 0;
     169
     170        PCUT_ASSERT_STR_EQUALS("hello", seq);
     171        PCUT_ASSERT_STR_EQUALS("w", pseq);
     172
     173        free(pseq);
     174}
     175
    64176#endif
    65177
Note: See TracChangeset for help on using the changeset viewer.