home *** CD-ROM | disk | FTP | other *** search
/ ftp.ncftp.com / ftp.ncftp.com.zip / ftp.ncftp.com / libncftp / older_versions / libncftp-3.2.2-src.tar.bz2 / libncftp-3.2.2-src.tar / libncftp-3.2.2 / Strn / tester.c < prev   
C/C++ Source or Header  |  2001-12-16  |  11KB  |  533 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <Strn.h>
  5.  
  6. int main(int argc, char **argv)
  7. {
  8.     char a[8];
  9.     char *b;
  10.     char *str;
  11.     const char *cp;
  12.     char c[37];
  13.     int i;
  14.     int len1, len2;
  15.     int failures = 0;
  16.     DStr ds;
  17.  
  18.     b = Strncpy(a, "hello", sizeof(a));
  19.     b = Strncat(b, "world", sizeof(a));
  20.     if (memcmp(b, "hellowo", 7 + 1) != 0) {
  21.         ++failures;
  22.         printf("1: fail\n");
  23.         printf("1: result=[%s] should be=[%s]\n",
  24.             b,
  25.             "hellowo"
  26.         );
  27.     }
  28.  
  29.     for (i=0; i<(int) sizeof(c); i++)
  30.         c[i] = 'X';
  31.     b = Strncpy(c, "testing", sizeof(c) - 2);
  32. #if (STRN_ZERO_PAD == 1)
  33.     for (i=7; i<(int) sizeof(c) - 2; i++) {
  34.         if (c[i] != '\0') {
  35.             ++failures;
  36.             printf("2: did not clear to end of buffer\n");
  37.             break;
  38.         }
  39.     }
  40. #endif
  41.     for (i=(int) sizeof(c) - 2; i<(int) sizeof(c); i++) {
  42.         if (c[i] != 'X') {
  43.             ++failures;
  44.             printf("2: overwrote buffer\n");
  45.             break;
  46.         }
  47.     }
  48.  
  49.     for (i=0; i<(int) sizeof(c); i++)
  50.         c[i] = 'X';
  51.     b = Strncpy(c, "testing", sizeof(c) - 2);
  52.     b = Strncat(b, " still", sizeof(c) - 2);
  53. #if (STRN_ZERO_PAD == 1)
  54.     for (i=13; i<(int) sizeof(c) - 2; i++) {
  55.         if (c[i] != '\0') {
  56.             ++failures;
  57.             printf("3: did not clear to end of buffer\n");
  58.             break;
  59.         }
  60.     }
  61. #endif
  62.     for (i=(int) sizeof(c) - 2; i<(int) sizeof(c); i++) {
  63.         if (c[i] != 'X') {
  64.             ++failures;
  65.             printf("3: overwrote buffer\n");
  66.             break;
  67.         }
  68.     }
  69.  
  70. /*--------------*/
  71.  
  72.     b = Strnpcpy(a, "hello", sizeof(a));
  73.     len1 = (int) (b - a);
  74.     b = Strnpcat(a, "world", sizeof(a));
  75.     len2 = (int) (b - a);
  76.     if ((len1 != 5) || (len2 != 7) || (strcmp(a, "hellowo") != 0)) {
  77.         ++failures;
  78.         printf("4: fail\n");
  79.         printf("4: result=[%s] should be=[%s] len1=%d len2=%d\n",
  80.             a,
  81.             "hellowo",
  82.             len1,
  83.             len2
  84.         );
  85.     }
  86.     for (i=0; i<(int) sizeof(c); i++)
  87.         c[i] = 'X';
  88.     b = Strnpcpy(c, "testing", sizeof(c) - 2);
  89. #if (STRNP_ZERO_PAD == 1)
  90.     for (i=7; i<(int) sizeof(c) - 2; i++) {
  91.         if (c[i] != '\0') {
  92.             ++failures;
  93.             printf("5: did not clear to end of buffer\n");
  94.             break;
  95.         }
  96.     }
  97. #endif
  98.     for (i=(int) sizeof(c) - 2; i<(int) sizeof(c); i++) {
  99.         if (c[i] != 'X') {
  100.             ++failures;
  101.             printf("5: overwrote buffer\n");
  102.             break;
  103.         }
  104.     }
  105.  
  106.     for (i=0; i<(int) sizeof(c); i++)
  107.         c[i] = 'X';
  108.     b = Strnpcpy(c, "testing", sizeof(c) - 2);
  109.     b = Strnpcat(c, " still", sizeof(c) - 2);
  110. #if (STRNP_ZERO_PAD == 1)
  111.     for (i=13; i<(int) sizeof(c) - 2; i++) {
  112.         if (c[i] != '\0') {
  113.             ++failures;
  114.             printf("6: did not clear to end of buffer\n");
  115.             break;
  116.         }
  117.     }
  118. #endif
  119.     for (i=(int) sizeof(c) - 2; i<(int) sizeof(c); i++) {
  120.         if (c[i] != 'X') {
  121.             ++failures;
  122.             printf("6: overwrote buffer\n");
  123.             break;
  124.         }
  125.     }
  126.  
  127.     str = NULL;
  128.     if (Dynscat(&str, "this is a test", 0) == NULL) {
  129.         ++failures;
  130.         printf("7a: fail\n");
  131.     } else if (strcmp(str, "this is a test") != 0) {
  132.         ++failures;
  133.         printf("7b: fail\n");
  134.     }
  135.     free(str);
  136.  
  137.     str = NULL;
  138.     if (Dynscat(&str, "this is a test", 0) == NULL) {
  139.         ++failures;
  140.         printf("7c: fail\n");
  141.     } else if (strcmp(str, "this is a test") != 0) {
  142.         ++failures;
  143.         printf("7d: fail\n");
  144.     } else if (Dynscat(&str, " ", "", "and", " ", "so is this", 0) == NULL) {
  145.         ++failures;
  146.         printf("7e: fail\n");
  147.     } else if (strcmp(str, "this is a test and so is this") != 0) {
  148.         ++failures;
  149.         printf("7f: fail\n");
  150.     }
  151.     free(str);
  152.  
  153.     str = NULL;
  154.     Dynscpy(&str, "test", 0);
  155.     if (strcmp(str, "test") != 0) {
  156.         ++failures;
  157.         printf("8: fail\n");
  158.     }
  159. #if 0    /* no longer supported */
  160.     Dynscpy(&str, "This is a ", str, ".",  0);
  161.     if (strcmp(str, "This is a test.") != 0) {
  162.         ++failures;
  163.         printf("9: fail\n");
  164.     }
  165. #endif
  166.     free(str);
  167.  
  168.     str = NULL;
  169.     Dynsrecpy(&str, "cruel", 0);
  170.     Dynsrecpy(&str, "hello, ", str, " world!", 0);
  171.     if (strcmp(str, "hello, cruel world!") != 0) {
  172.         ++failures;
  173.         printf("10: fail\n");
  174.     } else {
  175.         StrFree(&str);
  176.         if (str != NULL) {
  177.             ++failures;
  178.             printf("11: fail\n");
  179.         }
  180.     }
  181.  
  182.     str = NULL;
  183.     Dynscat(&str, "cruel", 0);
  184.     if ((Dynscat(&str, "hello ", str, " world!") != NULL) || (str != NULL)) {
  185.         ++failures;
  186.         printf("12: fail\n");
  187.     }
  188.  
  189.     DStrInit(&ds);
  190.     DStrFree(&ds);    /* Should be able to do this now */
  191.  
  192.     if (DStrNew(&ds, 20) < 0) {
  193.         ++failures;
  194.         printf("13a: fail\n");
  195.     } else {
  196.         if (ds.allocSize != 32) {
  197.             printf("13b: fail (%u)\n", ds.allocSize);
  198.             ++failures;
  199.         }
  200.         if (ds.len != 0) {
  201.             printf("13c: fail (%u)\n", ds.len);
  202.             ++failures;
  203.         }
  204.         if ((((int) ds.s) & 1) != 0) {
  205.             printf("13d: fail (%p)\n", ds.s);
  206.             ++failures;
  207.         }
  208.         DStrFree(&ds);
  209.         if (ds.s != NULL) {
  210.             printf("13e: fail (%p)\n", ds.s);
  211.             ++failures;
  212.         }
  213.     }
  214.  
  215.     cp = DStrCpy(&ds, "the quick brown fox jumped over the lazy dog");
  216.     if (cp == NULL) {
  217.         ++failures;
  218.         printf("14a: fail\n");
  219.     } else {
  220.         if (ds.allocSize != 48) {
  221.             printf("14b: fail (%u)\n", ds.allocSize);
  222.             ++failures;
  223.         }
  224.         if (ds.len != 44) {
  225.             printf("14c: fail (%u)\n", ds.len);
  226.             ++failures;
  227.         }
  228.         if ((((int) ds.s) & 1) != 0) {
  229.             printf("14d: fail (%p)\n", ds.s);
  230.             ++failures;
  231.         }
  232.         if (strcmp(ds.s, "the quick brown fox jumped over the lazy dog") != 0) {
  233.             printf("14e: fail (%s)\n", ds.s);
  234.             ++failures;
  235.         }
  236.         if (cp != ds.s) {
  237.             printf("14f: fail (%s)\n", ds.s);
  238.             ++failures;
  239.         }
  240.         cp = DStrCat(&ds, " and then died of a massive heart attack");
  241.         if (cp == NULL) {
  242.             ++failures;
  243.             printf("15a: fail\n");
  244.         } else {
  245.             if (ds.allocSize != 96) {
  246.                 printf("15b: fail (%u)\n", ds.allocSize);
  247.                 ++failures;
  248.             }
  249.             if (ds.len != 84) {
  250.                 printf("15c: fail (%u)\n", ds.len);
  251.                 ++failures;
  252.             }
  253.             if ((((int) ds.s) & 1) != 0) {
  254.                 printf("15d: fail (%p)\n", ds.s);
  255.                 ++failures;
  256.             }
  257.             if (strcmp(ds.s, "the quick brown fox jumped over the lazy dog and then died of a massive heart attack") != 0) {
  258.                 printf("15e: fail (%s)\n", ds.s);
  259.                 ++failures;
  260.             }
  261.             if (cp != ds.s) {
  262.                 printf("15f: fail (%s)\n", ds.s);
  263.                 ++failures;
  264.             }
  265.             cp = DStrCat(&ds, " (EOLN)");
  266.             if (cp == NULL) {
  267.                 ++failures;
  268.                 printf("16a: fail\n");
  269.             } else {
  270.                 if (ds.allocSize != 96) {
  271.                     printf("16b: fail (%u)\n", ds.allocSize);
  272.                     ++failures;
  273.                 }
  274.                 if (ds.len != 91) {
  275.                     printf("16c: fail (%u)\n", ds.len);
  276.                     ++failures;
  277.                 }
  278.                 if ((((int) ds.s) & 1) != 0) {
  279.                     printf("16d: fail (%p)\n", ds.s);
  280.                     ++failures;
  281.                 }
  282.                 if (strcmp(ds.s, "the quick brown fox jumped over the lazy dog and then died of a massive heart attack (EOLN)") != 0) {
  283.                     printf("16e: fail (%s)\n", ds.s);
  284.                     ++failures;
  285.                 }
  286.                 if (cp != ds.s) {
  287.                     printf("16f: fail (%s)\n", ds.s);
  288.                     ++failures;
  289.                 }
  290.                 (void) DStrCat(&ds, "123");
  291.                 if (ds.allocSize != 96) {
  292.                     printf("17a: fail (%u)\n", ds.allocSize);
  293.                     ++failures;
  294.                 }
  295.                 if (ds.len != 94) {
  296.                     printf("17b: fail (%u)\n", ds.len);
  297.                     ++failures;
  298.                 }
  299.                 if (strcmp(ds.s, "the quick brown fox jumped over the lazy dog and then died of a massive heart attack (EOLN)123") != 0) {
  300.                     printf("17c: fail (%s)\n", ds.s);
  301.                     ++failures;
  302.                 }
  303.                 (void) DStrCat(&ds, "4");
  304.                 if (ds.allocSize != 96) {
  305.                     printf("18a: fail (%u)\n", ds.allocSize);
  306.                     ++failures;
  307.                 }
  308.                 if (ds.len != 95) {
  309.                     printf("18b: fail (%u)\n", ds.len);
  310.                     ++failures;
  311.                 }
  312.                 if (strcmp(ds.s, "the quick brown fox jumped over the lazy dog and then died of a massive heart attack (EOLN)1234") != 0) {
  313.                     printf("18c: fail (%s)\n", ds.s);
  314.                     ++failures;
  315.                 }
  316.                 (void) DStrCat(&ds, "5");
  317.                 if (ds.allocSize != 112) {
  318.                     printf("19a: fail (%u)\n", ds.allocSize);
  319.                     ++failures;
  320.                 }
  321.                 if (ds.len != 96) {
  322.                     printf("19b: fail (%u)\n", ds.len);
  323.                     ++failures;
  324.                 }
  325.                 if (strcmp(ds.s, "the quick brown fox jumped over the lazy dog and then died of a massive heart attack (EOLN)12345") != 0) {
  326.                     printf("19c: fail (%s)\n", ds.s);
  327.                     ++failures;
  328.                 }
  329.             }
  330.         }
  331.         DStrFree(&ds);
  332.         if (ds.s != NULL) {
  333.             printf("14g: fail (%p)\n", ds.s);
  334.             ++failures;
  335.         }
  336.     }
  337.  
  338.     cp = DStrCpyList(&ds,
  339.         "the",
  340.         " quick",
  341.         " brown",
  342.         " fox",
  343.         " jumped",
  344.         " over",
  345.         " the",
  346.         " lazy",
  347.         " dog",
  348.         0
  349.     );
  350.     if (cp == NULL) {
  351.         ++failures;
  352.         printf("20a: fail\n");
  353.     } else {
  354.         if (ds.allocSize != 48) {
  355.             printf("20b: fail (%u)\n", ds.allocSize);
  356.             ++failures;
  357.         }
  358.         if (ds.len != 44) {
  359.             printf("20c: fail (%u)\n", ds.len);
  360.             ++failures;
  361.         }
  362.         if ((((int) ds.s) & 1) != 0) {
  363.             printf("20d: fail (%p)\n", ds.s);
  364.             ++failures;
  365.         }
  366.         if (strcmp(ds.s, "the quick brown fox jumped over the lazy dog") != 0) {
  367.             printf("20e: fail (%s)\n", ds.s);
  368.             ++failures;
  369.         }
  370.         if (cp != ds.s) {
  371.             printf("20f: fail (%s)\n", ds.s);
  372.             ++failures;
  373.         }
  374.         DStrFree(&ds);
  375.     }
  376.  
  377.     (void) DStrCpy(&ds, "the");
  378.     cp = DStrCatList(&ds,
  379.         " quick",
  380.         " brown",
  381.         " fox",
  382.         " jumped",
  383.         " over",
  384.         " the",
  385.         " lazy",
  386.         " dog",
  387.         0
  388.     );
  389.     if (cp == NULL) {
  390.         ++failures;
  391.         printf("21a: fail\n");
  392.     } else {
  393.         if (ds.allocSize != 48) {
  394.             printf("21b: fail (%u)\n", ds.allocSize);
  395.             ++failures;
  396.         }
  397.         if (ds.len != 44) {
  398.             printf("21c: fail (%u)\n", ds.len);
  399.             ++failures;
  400.         }
  401.         if ((((int) ds.s) & 1) != 0) {
  402.             printf("21d: fail (%p)\n", ds.s);
  403.             ++failures;
  404.         }
  405.         if (strcmp(ds.s, "the quick brown fox jumped over the lazy dog") != 0) {
  406.             printf("21e: fail (%s)\n", ds.s);
  407.             ++failures;
  408.         }
  409.         if (cp != ds.s) {
  410.             printf("21f: fail (%s)\n", ds.s);
  411.             ++failures;
  412.         }
  413.         DStrFree(&ds);
  414.     }
  415.  
  416.     (void) DStrCpy(&ds, "fox");
  417.     cp = DStrCpyList(&ds,
  418.         "the",
  419.         " quick",
  420.         " brown ",
  421.         ds.s,
  422.         " jumped",
  423.         " over",
  424.         " the",
  425.         " lazy",
  426.         " dog",
  427.         0
  428.     );
  429.     if (cp == NULL) {
  430.         ++failures;
  431.         printf("22a: fail\n");
  432.     } else {
  433.         if (ds.allocSize != 48) {
  434.             printf("22b: fail (%u)\n", ds.allocSize);
  435.             ++failures;
  436.         }
  437.         if (ds.len != 44) {
  438.             printf("22c: fail (%u)\n", ds.len);
  439.             ++failures;
  440.         }
  441.         if ((((int) ds.s) & 1) != 0) {
  442.             printf("22d: fail (%p)\n", ds.s);
  443.             ++failures;
  444.         }
  445.         if (strcmp(ds.s, "the quick brown fox jumped over the lazy dog") != 0) {
  446.             printf("22e: fail (%s)\n", ds.s);
  447.             ++failures;
  448.         }
  449.         if (cp != ds.s) {
  450.             printf("22f: fail (%s)\n", ds.s);
  451.             ++failures;
  452.         }
  453.         DStrFree(&ds);
  454.     }
  455.  
  456.     (void) DStrCpy(&ds, "the");
  457.     cp = DStrCatList(&ds,
  458.         " quick",
  459.         " brown",
  460.         " fox",
  461.         " jumped",
  462.         " over",
  463.         " ",
  464.         ds.s,
  465.         " lazy",
  466.         " dog",
  467.         0
  468.     );
  469.     if (cp == NULL) {
  470.         ++failures;
  471.         printf("23a: fail\n");
  472.     } else {
  473.         if (ds.allocSize != 48) {
  474.             printf("23b: fail (%u)\n", ds.allocSize);
  475.             ++failures;
  476.         }
  477.         if (ds.len != 44) {
  478.             printf("23c: fail (%u)\n", ds.len);
  479.             ++failures;
  480.         }
  481.         if ((((int) ds.s) & 1) != 0) {
  482.             printf("23d: fail (%p)\n", ds.s);
  483.             ++failures;
  484.         }
  485.         if (strcmp(ds.s, "the quick brown fox jumped over the lazy dog") != 0) {
  486.             printf("23e: fail (%s)\n", ds.s);
  487.             ++failures;
  488.         }
  489.         if (cp != ds.s) {
  490.             printf("23f: fail (%s)\n", ds.s);
  491.             ++failures;
  492.         }
  493.         DStrFree(&ds);
  494.     }
  495.  
  496.     cp = DStrCpy(&ds, "1234567890");
  497.     if (cp == NULL) {
  498.         ++failures;
  499.         printf("24a: fail\n");
  500.     } else {
  501.         cp = DStrCat(&ds, ds.s);
  502.         if (cp == NULL) {
  503.             ++failures;
  504.             printf("24b: fail\n");
  505.         } else {
  506.             if (ds.allocSize != 32) {
  507.                 printf("23c: fail (%u)\n", ds.allocSize);
  508.                 ++failures;
  509.             }
  510.             if (ds.len != 20) {
  511.                 printf("23d: fail (%u)\n", ds.len);
  512.                 ++failures;
  513.             }
  514.             if ((((int) ds.s) & 1) != 0) {
  515.                 printf("23e: fail (%p)\n", ds.s);
  516.                 ++failures;
  517.             }
  518.             if (strcmp(ds.s, "12345678901234567890") != 0) {
  519.                 printf("23f: fail (%s)\n", ds.s);
  520.                 ++failures;
  521.             }
  522.             if (cp != ds.s) {
  523.                 printf("23g: fail (%s)\n", ds.s);
  524.                 ++failures;
  525.             }
  526.             DStrFree(&ds);
  527.         }
  528.     }
  529.  
  530.     printf("Done.  Number of tests failed: %d.\n", failures);
  531.     exit(0);
  532. }
  533.