Changeset 4e6a610 in mainline for uspace/lib/c/test/stdio.c


Ignore:
Timestamp:
2018-06-25T09:54:28Z (7 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
bfe90b6
Parents:
fb0ec570
git-author:
Jiri Svoboda <jiri@…> (2018-06-24 17:51:54)
git-committer:
Jiri Svoboda <jiri@…> (2018-06-25 09:54:28)
Message:

Temporary file functions rework. Fix libposix access() not working on directories.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/test/stdio.c

    rfb0ec570 r4e6a610  
    3838#include <pcut/pcut.h>
    3939#include <stdio.h>
     40#include <str.h>
     41#include <tmpfile.h>
     42#include <vfs/vfs.h>
    4043
    4144PCUT_INIT;
    4245
    4346PCUT_TEST_SUITE(stdio);
     47
     48/** remove function */
     49PCUT_TEST(remove)
     50{
     51        char buf[L_tmpnam];
     52        char *p;
     53        FILE *f;
     54        int rc;
     55
     56        /* Generate unique file name */
     57        p = tmpnam(buf);
     58        PCUT_ASSERT_NOT_NULL(p);
     59
     60        /* Removing it should fail */
     61        rc = remove(buf);
     62        PCUT_ASSERT_TRUE(rc != 0);
     63
     64        f = fopen(buf, "w");
     65        PCUT_ASSERT_NOT_NULL(f);
     66        fclose(f);
     67
     68        /* Remove for the first time */
     69        rc = remove(buf);
     70        PCUT_ASSERT_INT_EQUALS(0, rc);
     71
     72        /* Should fail the second time */
     73        rc = remove(buf);
     74        PCUT_ASSERT_TRUE(rc != 0);
     75}
     76
     77/** rename function */
     78PCUT_TEST(rename)
     79{
     80        char buf1[L_tmpnam];
     81        char buf2[L_tmpnam];
     82        char *p;
     83        FILE *f;
     84        int rc;
     85
     86        /* Generate first unique file name */
     87        p = tmpnam(buf1);
     88        PCUT_ASSERT_NOT_NULL(p);
     89
     90        /* Generate second unique file name */
     91        p = tmpnam(buf2);
     92        PCUT_ASSERT_NOT_NULL(p);
     93
     94        f = fopen(buf1, "w");
     95        PCUT_ASSERT_NOT_NULL(f);
     96        fclose(f);
     97
     98        /* Rename to the second name */
     99        rc = rename(buf1, buf2);
     100        PCUT_ASSERT_INT_EQUALS(0, rc);
     101
     102        /* First name should no longer exist */
     103        rc = remove(buf1);
     104        PCUT_ASSERT_TRUE(rc != 0);
     105
     106        /* Second can be removed */
     107        rc = remove(buf2);
     108        PCUT_ASSERT_INT_EQUALS(0, rc);
     109}
     110
     111/** tmpfile function */
     112PCUT_TEST(tmpfile)
     113{
     114        FILE *f;
     115        char c;
     116        size_t n;
     117
     118        f = tmpfile();
     119        PCUT_ASSERT_NOT_NULL(f);
     120
     121        c = 'x';
     122        n = fwrite(&c, sizeof(c), 1, f);
     123        PCUT_ASSERT_INT_EQUALS(1, n);
     124
     125        rewind(f);
     126        c = '\0';
     127        n = fread(&c, sizeof(c), 1, f);
     128        PCUT_ASSERT_INT_EQUALS(1, n);
     129        PCUT_ASSERT_INT_EQUALS('x', c);
     130
     131        fclose(f);
     132}
     133
     134/** tmpnam function with buffer argument */
     135PCUT_TEST(tmpnam_buf)
     136{
     137        char buf[L_tmpnam];
     138        char *p;
     139        FILE *f;
     140
     141        p = tmpnam(buf);
     142        PCUT_ASSERT_NOT_NULL(p);
     143
     144        f = fopen(p, "w+");
     145        PCUT_ASSERT_NOT_NULL(f);
     146        (void) remove(p);
     147        fclose(f);
     148}
     149
     150/** tmpnam function called twice */
     151PCUT_TEST(tmpnam_twice)
     152{
     153        char buf1[L_tmpnam];
     154        char buf2[L_tmpnam];
     155        char *p;
     156
     157        p = tmpnam(buf1);
     158        PCUT_ASSERT_NOT_NULL(p);
     159
     160        p = tmpnam(buf2);
     161        PCUT_ASSERT_NOT_NULL(p);
     162
     163        /* We must get two distinct names */
     164        PCUT_ASSERT_TRUE(str_cmp(buf1, buf2) != 0);
     165}
     166
     167/** tmpnam function with NULL argument */
     168PCUT_TEST(tmpnam_null)
     169{
     170        char *p;
     171        FILE *f;
     172
     173        p = tmpnam(NULL);
     174        PCUT_ASSERT_NOT_NULL(p);
     175
     176        f = fopen(p, "w+");
     177        PCUT_ASSERT_NOT_NULL(f);
     178        (void) remove(p);
     179        fclose(f);
     180}
    44181
    45182/** fgetpos and fsetpos functions */
     
    50187        FILE *f;
    51188
    52         // XXX Use tmpfile() when it is available
    53         f = fopen("/tmp/fgpsp.tmp", "w+");
    54         PCUT_ASSERT_NOT_NULL(f);
    55         rc = remove("/tmp/fgpsp.tmp");
    56         PCUT_ASSERT_INT_EQUALS(0, rc);
     189        f = tmpfile();
     190        PCUT_ASSERT_NOT_NULL(f);
    57191
    58192        rc = fputs("abc", f);
Note: See TracChangeset for help on using the changeset viewer.