LCOV - code coverage report
Current view: top level - libc/test - run-snprintf.c (source / functions) Hit Total Coverage
Test: skiboot.info Lines: 146 149 98.0 %
Date: 2024-01-02 21:04:04 Functions: 10 10 100.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
       2                 :            : /*
       3                 :            :  * Copyright 2013-2015 IBM Corp.
       4                 :            :  */
       5                 :            : 
       6                 :            : #define BUFSZ 50
       7                 :            : 
       8                 :            : #include <stdlib.h>
       9                 :            : #include <assert.h>
      10                 :            : #include <string.h>
      11                 :            : #include <stdio.h>
      12                 :            : 
      13                 :            : int test1(void);
      14                 :            : int skiboot_snprintf(char *buf, size_t bufsz, size_t l, const char* format, ...);
      15                 :            : 
      16                 :          5 : static void test_printf_0u(int n)
      17                 :            : {
      18                 :            :         char *buf, *buf2;
      19                 :            :         int blen;
      20                 :            :         unsigned int i;
      21                 :            : 
      22                 :         50 :         for(i=1; i<10; i++)
      23                 :            :         {
      24                 :         45 :                 blen = i+1;
      25                 :         45 :                 if (n<0)
      26                 :          0 :                         blen++;
      27                 :            : 
      28                 :         45 :                 buf = (char*)malloc(blen);
      29                 :         45 :                 buf2 = (char*)malloc(blen);
      30                 :         45 :                 skiboot_snprintf(buf, blen, blen, "%08u", n);
      31                 :         45 :                 snprintf(buf2, blen, "%08u", n);
      32                 :         45 :                 n = n * 10;
      33                 :         45 :                 assert(0 == strncmp(buf, buf2, blen));
      34                 :         45 :                 free(buf);
      35                 :         45 :                 free(buf2);
      36                 :            :         }
      37                 :          5 : }
      38                 :            : 
      39                 :          5 : static void test_printf_u(int n)
      40                 :            : {
      41                 :            :         char *buf, *buf2;
      42                 :            :         int blen;
      43                 :            :         unsigned int r;
      44                 :            :         unsigned int i;
      45                 :            : 
      46                 :         50 :         for(i=1; i<10; i++)
      47                 :            :         {
      48                 :         45 :                 blen = i+1;
      49                 :         45 :                 if (n<0)
      50                 :          0 :                         blen++;
      51                 :            : 
      52                 :         45 :                 buf = (char*)malloc(blen);
      53                 :         45 :                 buf2 = (char*)malloc(blen);
      54                 :         45 :                 r = skiboot_snprintf(buf, blen, blen, "%u", n);
      55                 :         45 :                 snprintf(buf2, blen, "%u", n);
      56                 :         45 :                 n = n * 10;
      57                 :         45 :                 if (n<0)
      58                 :          0 :                         assert(i+1 == r);
      59                 :            :                 else
      60                 :         45 :                         assert(i == r);
      61                 :         45 :                 assert(0 == strncmp(buf, buf2, blen));
      62                 :         45 :                 free(buf);
      63                 :         45 :                 free(buf2);
      64                 :            :         }
      65                 :          5 : }
      66                 :            : 
      67                 :         10 : static void test_printf_d(int n)
      68                 :            : {
      69                 :            :         char *buf, *buf2;
      70                 :            :         int blen;
      71                 :            :         int r;
      72                 :            :         int i;
      73                 :            : 
      74                 :        100 :         for(i=1; i<10; i++)
      75                 :            :         {
      76                 :         90 :                 blen = i+1;
      77                 :         90 :                 if (n<0)
      78                 :         45 :                         blen++;
      79                 :            : 
      80                 :         90 :                 buf = (char*)malloc(blen);
      81                 :         90 :                 buf2 = (char*)malloc(blen);
      82                 :         90 :                 r = skiboot_snprintf(buf, blen, blen, "%d", n);
      83                 :         90 :                 snprintf(buf2, blen, "%d", n);
      84                 :         90 :                 n = n * 10;
      85                 :         90 :                 if (n<0)
      86                 :         45 :                         assert(i+1 == r);
      87                 :            :                 else
      88                 :         45 :                         assert(i == r);
      89                 :         90 :                 assert(0 == strncmp(buf, buf2, blen));
      90                 :         90 :                 free(buf);
      91                 :         90 :                 free(buf2);
      92                 :            :         }
      93                 :         10 : }
      94                 :            : 
      95                 :         10 : static void test_printf_x(const char* f)
      96                 :            : {
      97                 :            :         char *buf, *buf2;
      98                 :            :         int blen;
      99                 :            :         int i, r;
     100                 :         10 :         unsigned int n=0x1;
     101                 :            : 
     102                 :         90 :         for (i=0; i<8; i++)
     103                 :            :         {
     104                 :         80 :                 blen = i+2;
     105                 :         80 :                 buf = (char*)malloc(blen);
     106                 :         80 :                 buf2 = (char*)malloc(blen);
     107                 :         80 :                 r = skiboot_snprintf(buf, blen, blen, f, n);
     108                 :         80 :                 snprintf(buf2, blen, f, n);
     109                 :         80 :                 assert(i+1 == r);
     110                 :         80 :                 assert(0 == strncmp(buf, buf2, blen));
     111                 :         80 :                 free(buf);
     112                 :         80 :                 free(buf2);
     113                 :         80 :                 n = n << 4;
     114                 :            :         }
     115                 :         10 : }
     116                 :            : 
     117                 :          5 : static void test_printf_c(void)
     118                 :            : {
     119                 :          5 :         char *buf= (char*)malloc(2);
     120                 :            :         char buf2[2];
     121                 :          5 :         unsigned char i= 0xff;
     122                 :            :         int r;
     123                 :       1280 :         while(i)
     124                 :            :         {
     125                 :       1275 :                 r = skiboot_snprintf(buf, 2, 2, "%c", i);
     126                 :       1275 :                 snprintf(buf2, 2, "%c", i);
     127                 :       1275 :                 assert(r==1);
     128                 :       1275 :                 assert(0 == strncmp(buf, buf2, 2));
     129                 :       1275 :                 i--;
     130                 :            :         }
     131                 :          5 :         free(buf);
     132                 :          5 : }
     133                 :            : 
     134                 :          5 : static void test_printf_p(void)
     135                 :            : {
     136                 :          5 :         char *buf= (char*)malloc(32);
     137                 :            :         char buf2[32];
     138                 :          5 :         skiboot_snprintf(buf, 32, 32, "%p", buf);
     139                 :          5 :         snprintf(buf2, 32, "%p", buf);
     140                 :          5 :         assert(0 == strncmp(buf, buf2, 32));
     141                 :          5 :         free(buf);
     142                 :          5 : }
     143                 :            : 
     144                 :          5 : static void test_printf_o(void)
     145                 :            : {
     146                 :          5 :         char *buf= (char*)malloc(32);
     147                 :            :         char buf2[32];
     148                 :          5 :         skiboot_snprintf(buf, 32, 32, "%o", 0x12345678);
     149                 :          5 :         snprintf(buf2, 32, "%o", 0x12345678);
     150                 :          5 :         assert(0 == strncmp(buf, buf2, 32));
     151                 :          5 :         free(buf);
     152                 :          5 : }
     153                 :            : 
     154                 :         35 : static void test_printf_h(short i)
     155                 :            : {
     156                 :         35 :         char *buf= (char*)malloc(32);
     157                 :            :         char buf2[32];
     158                 :         35 :         skiboot_snprintf(buf, 32, 32, "%hd", i);
     159                 :         35 :         snprintf(buf2, 32, "%hd", i);
     160                 :         35 :         assert(0 == strncmp(buf, buf2, 32));
     161                 :         35 :         free(buf);
     162                 :         35 : }
     163                 :            : 
     164                 :         20 : static void test_printf_z(size_t i)
     165                 :            : {
     166                 :         20 :         char *buf= (char*)malloc(32);
     167                 :            :         char buf2[32];
     168                 :         20 :         skiboot_snprintf(buf, 32, 32, "%zu", i);
     169                 :         20 :         snprintf(buf2, 32, "%zu", i);
     170                 :         20 :         assert(0 == strncmp(buf, buf2, 32));
     171                 :         20 :         free(buf);
     172                 :         20 : }
     173                 :            : 
     174                 :          5 : int main(void)
     175                 :            : {
     176                 :            :         char *buf;
     177                 :            :         int r;
     178                 :            : 
     179                 :          5 :         buf = (char*)malloc(BUFSZ);
     180                 :          5 :         memset(buf, 0, BUFSZ);
     181                 :            : 
     182                 :          5 :         assert(-1 == test1());
     183                 :            : 
     184                 :          5 :         r = skiboot_snprintf(buf, BUFSZ, 2, "%%");
     185                 :          5 :         assert(r==1);
     186                 :          5 :         assert(buf[0] == '%' && buf[1] == 0);
     187                 :            : 
     188                 :          5 :         r = skiboot_snprintf(buf, BUFSZ, 2, "%d", 137);
     189                 :            :         /* BUG/FIXME:
     190                 :            :          * skiboot libc does NOT return the length of the buffer you'd need
     191                 :            :          * Instead, it'll return something random, possibly zero (as here)
     192                 :            :          * but as you'll see in test_in_buf_len2, sometimes not.
     193                 :            :          *
     194                 :            :          * Basically, we're not POSIX printf and this is some day going to
     195                 :            :          * cause things to be awful.
     196                 :            :          */
     197                 :          5 :         assert(0 == r); // BUG, should be 3
     198                 :          5 :         assert(0 == strncmp(buf, "", 3));
     199                 :            : 
     200                 :          5 :         r = skiboot_snprintf(buf, BUFSZ, 4, "%d", 137);
     201                 :          5 :         assert(3 == r);
     202                 :          5 :         assert(0 == strncmp(buf, "137", 3));
     203                 :          5 :         assert(buf[3] == 0);
     204                 :            : 
     205                 :            :         /* Now we test the strange behaviour of our printf.
     206                 :            :          * For strings, we get partial prints going, but if we whack an
     207                 :            :          * integer on the end, we may or may not get that integer, depending
     208                 :            :          * on if we have enough size. We should test that though */
     209                 :            : 
     210                 :          5 :         r = skiboot_snprintf(buf, BUFSZ, 4, "Hello %d", 137);
     211                 :          5 :         assert(3 == r);
     212                 :          5 :         assert(0 == strncmp(buf, "Hel", 3));
     213                 :          5 :         assert(buf[3] == 0);
     214                 :          5 :         r = skiboot_snprintf(buf, BUFSZ, 7, "Hello %d", 137);
     215                 :          5 :         assert(6 == r);
     216                 :          5 :         assert(0 == strncmp(buf, "Hello ", 6));
     217                 :          5 :         assert(buf[6] == 0);
     218                 :          5 :         r = skiboot_snprintf(buf, BUFSZ, 10, "Hello %d", 137);
     219                 :          5 :         assert(9 == r);
     220                 :          5 :         assert(0 == strncmp(buf, "Hello 137", 10));
     221                 :          5 :         assert(buf[9] == 0);
     222                 :          5 :         free(buf);
     223                 :            : 
     224                 :          5 :         test_printf_u(1);
     225                 :          5 :         test_printf_0u(1);
     226                 :          5 :         test_printf_d(1);
     227                 :          5 :         test_printf_d(-1);
     228                 :          5 :         test_printf_x("%x");
     229                 :          5 :         test_printf_x("%X");
     230                 :          5 :         test_printf_c();
     231                 :          5 :         test_printf_p();
     232                 :          5 :         test_printf_o();
     233                 :          5 :         test_printf_h(0);
     234                 :          5 :         test_printf_h(128);
     235                 :          5 :         test_printf_h(256);
     236                 :          5 :         test_printf_h(-1);
     237                 :          5 :         test_printf_h(32767);
     238                 :          5 :         test_printf_h(32768);
     239                 :          5 :         test_printf_h(65535);
     240                 :          5 :         test_printf_z(0);
     241                 :          5 :         test_printf_z(-1);
     242                 :          5 :         test_printf_z(12345);
     243                 :          5 :         test_printf_z(128000000);
     244                 :            : 
     245                 :          5 :         return 0;
     246                 :            : }

Generated by: LCOV version 1.14