LCOV - code coverage report
Current view: top level - test - test-strxcpyx.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 62 62 100.0 %
Date: 2019-08-22 15:41:25 Functions: 7 7 100.0 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include <stdio.h>
       4             : #include <string.h>
       5             : 
       6             : #include "string-util.h"
       7             : #include "strxcpyx.h"
       8             : #include "util.h"
       9             : 
      10           1 : static void test_strpcpy(void) {
      11             :         char target[25];
      12           1 :         char *s = target;
      13             :         size_t space_left;
      14             : 
      15           1 :         space_left = sizeof(target);
      16           1 :         space_left = strpcpy(&s, space_left, "12345");
      17           1 :         space_left = strpcpy(&s, space_left, "hey hey hey");
      18           1 :         space_left = strpcpy(&s, space_left, "waldo");
      19           1 :         space_left = strpcpy(&s, space_left, "ba");
      20           1 :         space_left = strpcpy(&s, space_left, "r");
      21           1 :         space_left = strpcpy(&s, space_left, "foo");
      22             : 
      23           1 :         assert_se(streq(target, "12345hey hey heywaldobar"));
      24           1 :         assert_se(space_left == 0);
      25           1 : }
      26             : 
      27           1 : static void test_strpcpyf(void) {
      28             :         char target[25];
      29           1 :         char *s = target;
      30             :         size_t space_left;
      31             : 
      32           1 :         space_left = sizeof(target);
      33           1 :         space_left = strpcpyf(&s, space_left, "space left: %zu. ", space_left);
      34           1 :         space_left = strpcpyf(&s, space_left, "foo%s", "bar");
      35             : 
      36           1 :         assert_se(streq(target, "space left: 25. foobar"));
      37           1 :         assert_se(space_left == 3);
      38             : 
      39             :         /* test overflow */
      40           1 :         s = target;
      41           1 :         space_left = strpcpyf(&s, 12, "00 left: %i. ", 999);
      42           1 :         assert_se(streq(target, "00 left: 99"));
      43           1 :         assert_se(space_left == 0);
      44           1 :         assert_se(target[12] == '2');
      45           1 : }
      46             : 
      47           1 : static void test_strpcpyl(void) {
      48             :         char target[25];
      49           1 :         char *s = target;
      50             :         size_t space_left;
      51             : 
      52           1 :         space_left = sizeof(target);
      53           1 :         space_left = strpcpyl(&s, space_left, "waldo", " test", " waldo. ", NULL);
      54           1 :         space_left = strpcpyl(&s, space_left, "Banana", NULL);
      55             : 
      56           1 :         assert_se(streq(target, "waldo test waldo. Banana"));
      57           1 :         assert_se(space_left == 1);
      58           1 : }
      59             : 
      60           1 : static void test_strscpy(void) {
      61             :         char target[25];
      62             :         size_t space_left;
      63             : 
      64           1 :         space_left = sizeof(target);
      65           1 :         space_left = strscpy(target, space_left, "12345");
      66             : 
      67           1 :         assert_se(streq(target, "12345"));
      68           1 :         assert_se(space_left == 20);
      69           1 : }
      70             : 
      71           1 : static void test_strscpyl(void) {
      72             :         char target[25];
      73             :         size_t space_left;
      74             : 
      75           1 :         space_left = sizeof(target);
      76           1 :         space_left = strscpyl(target, space_left, "12345", "waldo", "waldo", NULL);
      77             : 
      78           1 :         assert_se(streq(target, "12345waldowaldo"));
      79           1 :         assert_se(space_left == 10);
      80           1 : }
      81             : 
      82           1 : static void test_sd_event_code_migration(void) {
      83             :         char b[100 * DECIMAL_STR_MAX(unsigned) + 1];
      84             :         char c[100 * DECIMAL_STR_MAX(unsigned) + 1], *p;
      85             :         unsigned i;
      86             :         size_t l;
      87             :         int o;
      88             : 
      89         101 :         for (i = o = 0; i < 100; i++)
      90         100 :                 o += snprintf(&b[o], sizeof(b) - o, "%u ", i);
      91             : 
      92           1 :         p = c;
      93           1 :         l = sizeof(c);
      94         101 :         for (i = 0; i < 100; i++)
      95         100 :                 l = strpcpyf(&p, l, "%u ", i);
      96             : 
      97           1 :         assert_se(streq(b, c));
      98           1 : }
      99             : 
     100           1 : int main(int argc, char *argv[]) {
     101           1 :         test_strpcpy();
     102           1 :         test_strpcpyf();
     103           1 :         test_strpcpyl();
     104           1 :         test_strscpy();
     105           1 :         test_strscpyl();
     106             : 
     107           1 :         test_sd_event_code_migration();
     108             : 
     109           1 :         return 0;
     110             : }

Generated by: LCOV version 1.14