github.com/Konstantin8105/c4go@v0.0.0-20240505174241-768bb1c65a51/tests/unistd.c (about)

     1  #include "tests.h"
     2  #include <fcntl.h>
     3  #include <stdio.h>
     4  #include <stdlib.h>
     5  #include <sys/stat.h>
     6  #include <sys/types.h>
     7  #include <unistd.h>
     8  
     9  #define START_TEST(t) \
    10      diag(#t);         \
    11      test_##t();
    12  
    13  #define MSGSIZE 16
    14  char* msg1 = "hello, world #1";
    15  char* msg2 = "hello, world #2";
    16  char* msg3 = "hello, world #3";
    17  
    18  void test_write()
    19  {
    20      char inbuf[MSGSIZE];
    21      for (int i = 0; i < MSGSIZE; i++) {
    22          inbuf[i] = '\x00';
    23      }
    24      int p[2] = { 0, 0 };
    25  
    26      if (pipe(p) < 0)
    27          exit(1);
    28  
    29      is_true(p[0] != p[1]);
    30  
    31      write(p[1], msg1, MSGSIZE);
    32      write(p[1], msg2, MSGSIZE);
    33      write(p[1], msg3, MSGSIZE);
    34  
    35      read(p[0], inbuf, MSGSIZE);
    36      is_streq(inbuf, msg1);
    37  
    38      read(p[0], inbuf, MSGSIZE);
    39      is_streq(inbuf, msg2);
    40  
    41      read(p[0], inbuf, MSGSIZE);
    42      is_streq(inbuf, msg3);
    43  }
    44  
    45  void test_read()
    46  {
    47      int fd = STDIN_FILENO;
    48      char data[128];
    49      for (int i = 0; i < 128; i++) {
    50          data[i] = '\x00';
    51      }
    52      printf("data = %s\n", data);
    53      is_streq(data, "");
    54      ssize_t s = read(fd, data, 128);
    55      if (s < 0) {
    56          fail("not good");
    57          write(2, "An error occurred in the read.\n", 31);
    58      }
    59      printf("data = %s\n", data);
    60      is_streq(data, "7");
    61      is_true(strlen(data) > 0);
    62  
    63      diag("wrong read");
    64      ssize_t sw = read(272727272, data, 128);
    65      is_eq(sw, -1);
    66  }
    67  
    68  void test_read_file()
    69  {
    70      int fd;
    71      ssize_t size_read;
    72      char buffer[200];
    73      fd = open("./tests/stdio.txt", O_RDONLY);
    74      size_read = read(fd, buffer, sizeof(buffer));
    75      if (size_read == -1) {
    76          fail("not ok");
    77          return;
    78      }
    79      is_true(size_read > 0);
    80      is_true(strlen(buffer) > 0);
    81      buffer[size_read] = '\x00';
    82      printf("buffer = `%s`\n", buffer);
    83      close(fd);
    84  }
    85  
    86  typedef struct {
    87      int left;
    88      int right;
    89  } pair_t;
    90  
    91  // void test_struct()
    92  // {
    93  // {// write
    94  // pair_t p;
    95  // p.left = 10;
    96  // p.right = 20;
    97  // write(1, &p, sizeof(pair_t));
    98  // }
    99  // { // read
   100  // pair_t p;
   101  // read(0, &p, sizeof(pair_t));
   102  // printf("left: %d right: %d\n",p.left, p.right);
   103  // }
   104  // }
   105  //
   106  
   107  void test_fstat()
   108  {
   109      char* filename = "./tests/stdio.txt";
   110  
   111      int file = 0;
   112      if ((file = open(filename, O_RDONLY)) < -1)
   113          return;
   114  
   115      struct stat fileStat;
   116      if (fstat(file, &fileStat) < 0)
   117          return;
   118  
   119      printf("Information for %s\n", filename);
   120      printf("---------------------------\n");
   121      printf("File Size: \t\t%d bytes\n", fileStat.st_size);
   122      printf("Number of Links: \t%d\n", fileStat.st_nlink);
   123      printf("File inode: \t\t%d\n", fileStat.st_ino);
   124  
   125      printf("File Permissions: \t");
   126      printf((S_ISDIR(fileStat.st_mode)) ? "d" : "-");
   127      printf((fileStat.st_mode & S_IRUSR) ? "r" : "-");
   128      printf((fileStat.st_mode & S_IWUSR) ? "w" : "-");
   129      printf((fileStat.st_mode & S_IXUSR) ? "x" : "-");
   130      printf((fileStat.st_mode & S_IRGRP) ? "r" : "-");
   131      printf((fileStat.st_mode & S_IWGRP) ? "w" : "-");
   132      printf((fileStat.st_mode & S_IXGRP) ? "x" : "-");
   133      printf((fileStat.st_mode & S_IROTH) ? "r" : "-");
   134      printf((fileStat.st_mode & S_IWOTH) ? "w" : "-");
   135      printf((fileStat.st_mode & S_IXOTH) ? "x" : "-");
   136      printf("\n\n");
   137  
   138      printf("The file %s a symbolic link\n\n", (S_ISLNK(fileStat.st_mode)) ? "is" : "is not");
   139  }
   140  
   141  int main()
   142  {
   143      plan(10);
   144  
   145      START_TEST(write);
   146      START_TEST(read);
   147      START_TEST(read_file);
   148      // START_TEST(struct);
   149      START_TEST(fstat);
   150  
   151      done_testing();
   152  }