Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. #define _BSD_SOURCE
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "test.h"
  6.  
  7. #ifndef min
  8. #define min(a,b) ((a<b)?a:b)
  9. #endif
  10.  
  11. size_t strlcpy(char *dst, const char *src, size_t size)
  12. {
  13.         int nsrc = strlen(src);
  14.     strncpy(dst, src, min(size, nsrc+1));
  15.     if (size > 0 && nsrc >= size)
  16.         dst[size - 1] = '\0';
  17.  
  18.         return nsrc;
  19. }
  20.  
  21. size_t strlcat(char *dst, const char *src, size_t size)
  22. {
  23.         int ndest = strlen(dst);
  24.         int nsrc = strlen(src);
  25.         if (size > ndest + 1)
  26.         {
  27.         strncat(dst, src, size - ndest - 1);
  28.         if (size > 0 && nsrc + ndest >= size)
  29.                 dst[size - 1] = '\0';
  30.     }
  31.  
  32.         return nsrc + ndest;
  33. }
  34.  
  35.  
  36.  
  37. /* r = place to store result
  38.  * f = function call to test (or any expression)
  39.  * x = expected result
  40.  * m = message to print on failure (with formats for r & x)
  41. **/
  42.  
  43. #define TEST(r, f, x, m) ( \
  44.         ((r) = (f)) == (x) || \
  45.         (t_error("%s failed (" m ")\n", #f, r, x), 0) )
  46.  
  47. #define TEST_S(s, x, m) ( \
  48.         !strcmp((s),(x)) || \
  49.         (t_error("[%s] != [%s] (%s)\n", s, x, m), 0) )
  50.  
  51. int main(void)
  52. {
  53.         char b[32];
  54.         char *s;
  55.         int i;
  56.  
  57.         b[16]='a'; b[17]='b'; b[18]='c'; b[19]=0;
  58.         TEST(s, strcpy(b, b+16), b, "wrong return %p != %p");
  59.         TEST_S(s, "abc", "strcpy gave incorrect string");
  60.         TEST(s, strcpy(b+1, b+16), b+1, "wrong return %p != %p");
  61.         TEST_S(s, "abc", "strcpy gave incorrect string");
  62.         TEST(s, strcpy(b+2, b+16), b+2, "wrong return %p != %p");
  63.         TEST_S(s, "abc", "strcpy gave incorrect string");
  64.         TEST(s, strcpy(b+3, b+16), b+3, "wrong return %p != %p");
  65.         TEST_S(s, "abc", "strcpy gave incorrect string");
  66.  
  67.         TEST(s, strcpy(b+1, b+17), b+1, "wrong return %p != %p");
  68.         TEST_S(s, "bc", "strcpy gave incorrect string");
  69.         TEST(s, strcpy(b+2, b+18), b+2, "wrong return %p != %p");
  70.         TEST_S(s, "c", "strcpy gave incorrect string");
  71.         TEST(s, strcpy(b+3, b+19), b+3, "wrong return %p != %p");
  72.         TEST_S(s, "", "strcpy gave incorrect string");
  73.  
  74.         TEST(s, memset(b, 'x', sizeof b), b, "wrong return %p != %p");
  75.         TEST(s, strncpy(b, "abc", sizeof b - 1), b, "wrong return %p != %p");
  76.         TEST(i, memcmp(b, "abc\0\0\0\0", 8), 0, "strncpy fails to zero-pad dest");
  77.         TEST(i, b[sizeof b - 1], 'x', "strncpy overruns buffer when n > strlen(src)");
  78.  
  79.         b[3] = 'x'; b[4] = 0;
  80.         strncpy(b, "abc", 3);
  81.         TEST(i, b[2], 'c', "strncpy fails to copy last byte: %hhu != %hhu");
  82.         TEST(i, b[3], 'x', "strncpy overruns buffer to null-terminate: %hhu != %hhu");
  83.  
  84.         TEST(i, !strncmp("abcd", "abce", 3), 1, "strncmp compares past n");
  85.         TEST(i, !!strncmp("abc", "abd", 3), 1, "strncmp fails to compare n-1st byte");
  86.  
  87.         strcpy(b, "abc");
  88.         TEST(s, strncat(b, "123456", 3), b, "%p != %p");
  89.         TEST(i, b[6], 0, "strncat failed to null-terminate (%d)");
  90.         TEST_S(s, "abc123", "strncat gave incorrect string");
  91.  
  92.         strcpy(b, "aaababccdd0001122223");
  93.         TEST(s, strchr(b, 'b'), b+3, "%p != %p");
  94.         TEST(s, strrchr(b, 'b'), b+5, "%p != %p");
  95.         TEST(i, strspn(b, "abcd"), 10, "%d != %d");
  96.         TEST(i, strcspn(b, "0123"), 10, "%d != %d");
  97.         TEST(s, strpbrk(b, "0123"), b+10, "%d != %d");
  98.  
  99.         strcpy(b, "abc   123; xyz; foo");
  100.         TEST(s, strtok(b, " "), b, "%p != %p");
  101.         TEST_S(s, "abc", "strtok result");
  102.  
  103.         TEST(s, strtok(NULL, ";"), b+4, "%p != %p");
  104.         TEST_S(s, "  123", "strtok result");
  105.  
  106.         TEST(s, strtok(NULL, " ;"), b+11, "%p != %p");
  107.         TEST_S(s, "xyz", "strtok result");
  108.  
  109.         TEST(s, strtok(NULL, " ;"), b+16, "%p != %p");
  110.         TEST_S(s, "foo", "strtok result");
  111.  
  112.         memset(b, 'x', sizeof b);
  113.         TEST(i, strlcpy(b, "abc", sizeof b - 1), 3, "length %d != %d");
  114.         TEST(i, b[3], 0, "strlcpy did not null-terminate short string (%d)");
  115.         TEST(i, b[4], 'x', "strlcpy wrote extra bytes (%d)");
  116.  
  117.         memset(b, 'x', sizeof b);
  118.         TEST(i, strlcpy(b, "abc", 2), 3, "length %d != %d");
  119.         TEST(i, b[0], 'a', "strlcpy did not copy character %d");
  120.         TEST(i, b[1], 0, "strlcpy did not null-terminate long string (%d)");
  121.  
  122.         memset(b, 'x', sizeof b);
  123.         TEST(i, strlcpy(b, "abc", 3), 3, "length %d != %d");
  124.         TEST(i, b[2], 0, "strlcpy did not null-terminate l-length string (%d)");
  125.  
  126.         TEST(i, strlcpy(NULL, "abc", 0), 3, "length %d != %d");
  127.  
  128.         memcpy(b, "abc\0\0\0x\0", 8);
  129.         TEST(i, strlcat(b, "123", sizeof b), 6, "length %d != %d");
  130.         TEST_S(b, "abc123", "strlcat result");
  131.  
  132.         memcpy(b, "abc\0\0\0x\0", 8);
  133.         TEST(i, strlcat(b, "123", 6), 6, "length %d != %d");
  134.         TEST_S(b, "abc12", "strlcat result");
  135.         TEST(i, b[6], 'x', "strlcat wrote past string %d != %d");
  136.  
  137.         memcpy(b, "abc\0\0\0x\0", 8);
  138.         TEST(i, strlcat(b, "123", 4), 6, "length %d != %d");
  139.         TEST_S(b, "abc", "strlcat result");
  140.  
  141.         memcpy(b, "abc\0\0\0x\0", 8);
  142.         TEST(i, strlcat(b, "123", 3), 6, "length %d != %d");
  143.         TEST_S(b, "abc", "strlcat result");
  144.  
  145.         printf("%s finished\n", __FILE__);
  146.         return t_status;
  147. }
  148.