LCOV - code coverage report
Current view: top level - ccan/str/test - run.c (source / functions) Hit Total Coverage
Test: skiboot.info Lines: 52 52 100.0 %
Date: 2024-01-02 21:04:04 Functions: 2 2 100.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : #include <ccan/str/str.h>
       2                 :            : #include <ccan/str/str.c>
       3                 :            : #include <stdlib.h>
       4                 :            : #include <stdio.h>
       5                 :            : #include <ccan/tap/tap.h>
       6                 :            : 
       7                 :            : #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
       8                 :            : 
       9                 :            : static const char *substrings[] = { "far", "bar", "baz", "b", "ba", "z", "ar",
      10                 :            :                                     NULL };
      11                 :            : 
      12                 :            : #define NUM_SUBSTRINGS (ARRAY_SIZE(substrings) - 1)
      13                 :            : 
      14                 :      14406 : static char *strdup_rev(const char *s)
      15                 :            : {
      16                 :      14406 :         char *ret = strdup(s);
      17                 :            :         unsigned int i;
      18                 :            : 
      19                 :      76146 :         for (i = 0; i < strlen(s); i++)
      20                 :      61740 :                 ret[i] = s[strlen(s) - i - 1];
      21                 :      14406 :         return ret;
      22                 :            : }
      23                 :            : 
      24                 :          3 : int main(void)
      25                 :            : {
      26                 :            :         unsigned int i, j, n;
      27                 :            :         char *strings[NUM_SUBSTRINGS * NUM_SUBSTRINGS];
      28                 :            :         
      29                 :          3 :         n = 0;
      30                 :         24 :         for (i = 0; i < NUM_SUBSTRINGS; i++) {
      31                 :        168 :                 for (j = 0; j < NUM_SUBSTRINGS; j++) {
      32                 :        147 :                         strings[n] = malloc(strlen(substrings[i])
      33                 :        147 :                                             + strlen(substrings[j]) + 1);
      34                 :        147 :                         sprintf(strings[n++], "%s%s",
      35                 :            :                                 substrings[i], substrings[j]);
      36                 :            :                 }
      37                 :            :         }
      38                 :            : 
      39                 :            :         plan_tests(n * n * 5 + 16);
      40                 :        150 :         for (i = 0; i < n; i++) {
      41                 :       7350 :                 for (j = 0; j < n; j++) {
      42                 :       7203 :                         unsigned int k, identical = 0;
      43                 :            :                         char *reva, *revb;
      44                 :            : 
      45                 :            :                         /* Find first difference. */
      46                 :      12957 :                         for (k = 0; strings[i][k]==strings[j][k]; k++) {
      47                 :       5901 :                                 if (k == strlen(strings[i])) {
      48                 :        147 :                                         identical = 1;
      49                 :        147 :                                         break;
      50                 :            :                                 }
      51                 :            :                         }
      52                 :            : 
      53                 :       7203 :                         if (identical) 
      54                 :        147 :                                 ok1(streq(strings[i], strings[j]));
      55                 :            :                         else
      56                 :       7056 :                                 ok1(!streq(strings[i], strings[j]));
      57                 :            : 
      58                 :            :                         /* Postfix test should be equivalent to prefix
      59                 :            :                          * test on reversed string. */
      60                 :       7203 :                         reva = strdup_rev(strings[i]);
      61                 :       7203 :                         revb = strdup_rev(strings[j]);
      62                 :            : 
      63                 :       7203 :                         if (!strings[i][k]) {
      64                 :        294 :                                 ok1(strstarts(strings[j], strings[i]));
      65                 :        294 :                                 ok1(strends(revb, reva));
      66                 :            :                         } else {
      67                 :       6909 :                                 ok1(!strstarts(strings[j], strings[i]));
      68                 :       6909 :                                 ok1(!strends(revb, reva));
      69                 :            :                         }
      70                 :       7203 :                         if (!strings[j][k]) {
      71                 :        294 :                                 ok1(strstarts(strings[i], strings[j]));
      72                 :        294 :                                 ok1(strends(reva, revb));
      73                 :            :                         } else {
      74                 :       6909 :                                 ok1(!strstarts(strings[i], strings[j]));
      75                 :       6909 :                                 ok1(!strends(reva, revb));
      76                 :            :                         }
      77                 :       7203 :                         free(reva);
      78                 :       7203 :                         free(revb);
      79                 :            :                 }
      80                 :            :         }
      81                 :            : 
      82                 :        150 :         for (i = 0; i < n; i++)
      83                 :        147 :                 free(strings[i]);
      84                 :            : 
      85                 :            :         ok1(streq(stringify(NUM_SUBSTRINGS),
      86                 :            :                   "((sizeof(substrings) / sizeof(substrings[0])) - 1)"));
      87                 :            :         ok1(streq(stringify(ARRAY_SIZE(substrings)),
      88                 :            :                   "(sizeof(substrings) / sizeof(substrings[0]))"));
      89                 :            :         ok1(streq(stringify(i == 0), "i == 0"));
      90                 :            : 
      91                 :          3 :         ok1(strcount("aaaaaa", "b") == 0);
      92                 :          3 :         ok1(strcount("aaaaaa", "a") == 6);
      93                 :          3 :         ok1(strcount("aaaaaa", "aa") == 3);
      94                 :          3 :         ok1(strcount("aaaaaa", "aaa") == 2);
      95                 :          3 :         ok1(strcount("aaaaaa", "aaaa") == 1);
      96                 :          3 :         ok1(strcount("aaaaaa", "aaaaa") == 1);
      97                 :          3 :         ok1(strcount("aaaaaa", "aaaaaa") == 1);
      98                 :          3 :         ok1(strcount("aaa aaa", "b") == 0);
      99                 :          3 :         ok1(strcount("aaa aaa", "a") == 6);
     100                 :          3 :         ok1(strcount("aaa aaa", "aa") == 2);
     101                 :          3 :         ok1(strcount("aaa aaa", "aaa") == 2);
     102                 :          3 :         ok1(strcount("aaa aaa", "aaaa") == 0);
     103                 :          3 :         ok1(strcount("aaa aaa", "aaaaa") == 0);
     104                 :            : 
     105                 :          3 :         return exit_status();
     106                 :            : }                               

Generated by: LCOV version 1.14