home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / tests / string / string.c < prev   
Encoding:
C/C++ Source or Header  |  1988-07-22  |  5.7 KB  |  277 lines

  1. /* 
  2.  * string.c --
  3.  *
  4.  *    This file contains a program that exercises the string
  5.  *    library procedures.  Invoke it with no parameters;  it
  6.  *    will print messages on stderr for any problems it detects
  7.  *    with the string procedures.
  8.  *
  9.  * Copyright 1988 Regents of the University of California
  10.  * Permission to use, copy, modify, and distribute this
  11.  * software and its documentation for any purpose and without
  12.  * fee is hereby granted, provided that the above copyright
  13.  * notice appear in all copies.  The University of California
  14.  * makes no representations about the suitability of this
  15.  * software for any purpose.  It is provided "as is" without
  16.  * express or implied warranty.
  17.  */
  18.  
  19. #ifndef lint
  20. static char rcsid[] = "$Header: string.c,v 1.2 88/07/22 08:59:52 ouster Exp $ SPRITE (Berkeley)";
  21. #endif not lint
  22.  
  23. #include <stdio.h>
  24. #include <string.h>
  25.  
  26. #define error(string) \
  27.     fprintf(stderr, string); \
  28.     exit(1);
  29.  
  30. main()
  31. {
  32.     int result = 0;
  33.     char test[100];
  34.     void *x;
  35.  
  36.     /*
  37.      * strcmp
  38.      */
  39.  
  40.     if (strcmp("abcdef", "abcdef") != 0) {
  41.     error("strcmp error 1\n");
  42.     }
  43.     if (strcmp("abcd", "abcdef") >= 0) {
  44.     error("strcmp error 2\n");
  45.     }
  46.     if (strcmp("12345", "12354") >= 0) {
  47.     error("strcmp error 3\n");
  48.     }
  49.     if (strcmp("xyz", "abc") <= 0) {
  50.     error("strcmp error 4\n");
  51.     }
  52.  
  53.     /*
  54.      * strncmp
  55.      */
  56.  
  57.     if (strncmp("abc\0x", "abc\0y", 6) != 0) {
  58.     error("strncmp error 1\n");
  59.     }
  60.     if (strncmp("abcdefg", "abcxyz", 3) != 0) {
  61.     error("strncmp error 2\n");
  62.     }
  63.     if (strncmp("abcdefg", "abcxyz", 4) >= 0) {
  64.     error("strncmp error 3\n");
  65.     }
  66.     if (strncmp("abcdefg", "abcabc", 4) <= 0) {
  67.     error("strncmp error 4\n");
  68.     }
  69.  
  70.     /*
  71.      * strcpy
  72.      */
  73.  
  74.     if (strcpy(test, "") != test) {
  75.     error("strcpy error 1\n");
  76.     }
  77.     if (test[0] != 0) {
  78.     error("strcpy error 2\n");
  79.     }
  80.     strcpy(test, "abcdefg");
  81.     if (strcmp(test, "abcdefg") != 0) {
  82.     error("strcpy error 3\n");
  83.     }
  84.  
  85.     /*
  86.      * strncpy
  87.      */
  88.  
  89.     strcpy(test, "abcdefg");
  90.     if (strncpy(test, "xyzqrs", 3) != test) {
  91.     error("strncpy error 1\n");
  92.     }
  93.     if (bcmp(test, "xyzdefg", 8) != 0) {
  94.     error("strncpy error 2\n");
  95.     }
  96.     strncpy(test, "12", 6);
  97.     if (bcmp(test, "12\0\0\0\0g", 8) != 0) {
  98.     error("strncpy error 3\n");
  99.     }
  100.  
  101.     /*
  102.      * strcat
  103.      */
  104.  
  105.     strcpy(test, "qrs");
  106.     if (strcat(test, "tuv") != test) {
  107.     error("strcat error 1\n");
  108.     }
  109.     if (strcmp(test, "qrstuv") != 0) {
  110.     error("strcat error 2\n");
  111.     }
  112.     test[0] = 0;
  113.     strcat(test, "12345");
  114.     if (strcmp(test, "12345") != 0) {
  115.     error("strcat error 3\n");
  116.     }
  117.     strcat(test, "");
  118.     if (strcmp(test, "12345") != 0) {
  119.     error("strcat error 4\n");
  120.     }
  121.  
  122.     /*
  123.      * strncat
  124.      */
  125.  
  126.     strcpy(test, "Now is the time");
  127.     if (strncat(test, " for all good men", 5) != test) {
  128.     error("strncat error 1\n");
  129.     }
  130.     if (strcmp(test, "Now is the time for ") != 0) {
  131.     error("strncat error 2\n");
  132.     }
  133.     strcpy(test, "abc");
  134.     strncat(test, "xyz", 6);
  135.     if (strcmp(test, "abcxyz") != 0) {
  136.     error("strncat error 3\n");
  137.     }
  138.  
  139.     /*
  140.      * strchr & index
  141.      */
  142.  
  143.     strcpy(test, "abcabcabcdef");
  144.     if (strchr(test, 'a') != test) {
  145.     error("strchr error 1");
  146.     }
  147.     if (strchr(test, 'c') != &test[2]) {
  148.     error("strchr error 2");
  149.     }
  150.     if (strchr(test, 'x') != 0) {
  151.     error("strchr error 3");
  152.     }
  153.     if (strchr(test, 0) != &test[12]) {
  154.     error("strchr error 4");
  155.     }
  156.     if (index(test, 'a') != test) {
  157.     error("index error 1");
  158.     }
  159.     if (index(test, 'c') != &test[2]) {
  160.     error("index error 2");
  161.     }
  162.     if (index(test, 'x') != 0) {
  163.     error("index error 3");
  164.     }
  165.     if (index(test, 0) != &test[12]) {
  166.     error("index b error 4");
  167.     }
  168.  
  169.     /*
  170.      * strrchr & rindex
  171.      */
  172.  
  173.     if (strrchr(test, 'a') != &test[6]) {
  174.     error("strrchr error 1\n");
  175.     }
  176.     if (strrchr(test, 0) != &test[12]) {
  177.     error("strrchr error 2\n");
  178.     }
  179.     if (strrchr(test, 'x') != 0) {
  180.     error("strrchr error 3\n");
  181.     }
  182.     if (rindex(test, 'a') != &test[6]) {
  183.     error("rindex error 1\n");
  184.     }
  185.     if (rindex(test, 0) != &test[12]) {
  186.     error("rindex error 2\n");
  187.     }
  188.     if (rindex(test, 'x') != 0) {
  189.     error("rindex error 3\n");
  190.     }
  191.  
  192.     /*
  193.      * strstr
  194.      */
  195.  
  196.     strcpy(test, "Now is the time for all good men");
  197.     if (strstr(test, "Now") != test) {
  198.     error("strstr error 1\n");
  199.     }
  200.     if (strstr(test, "good men") != &test[24]) {
  201.     error("strstr error 2\n");
  202.     }
  203.     if (strstr(test, "now") != 0) {
  204.     error("strstr error 3\n");
  205.     }
  206.     if (strstr(test, "") != test) {
  207.     error("strstr error 4\n");
  208.     }
  209.  
  210.     /*
  211.      * strlen
  212.      */
  213.  
  214.     if (strlen("") != 0) {
  215.     error("strlen error 1\n");
  216.     }
  217.     if (strlen("abc") != 3) {
  218.     error("strlen error 2\n");
  219.     }
  220.     if (strlen("Now is the time") != 15) {
  221.     error("strlen error 3\n");
  222.     }
  223.  
  224.     /*
  225.      * strpbrk
  226.      */
  227.  
  228.     strcpy(test, "12341234567");
  229.     if (strpbrk(test, "4567") != &test[3]) {
  230.     error("strpbrk error 1\n");
  231.     }
  232.     if (strpbrk(test, "") != 0) {
  233.     error("strpbrk error 2\n");
  234.     }
  235.     if (strpbrk(test, "abcdefgh") != 0) {
  236.     error("strpbrk error 3\n");
  237.     }
  238.  
  239.     /*
  240.      * strspn
  241.      */
  242.  
  243.     strcpy(test, "Now is the time for");
  244.     if (strspn(test, " ") != 0) {
  245.     error("strspn error 1\n");
  246.     }
  247.     if (strspn(test, "wnoNsi ") != 7) {
  248.     error("strspn error 2\n");
  249.     }
  250.     if (strspn(test, "") != 0) {
  251.     error("strspn error 3\n");
  252.     }
  253.     if (strspn(test, "aeioufhmNrstw ") != 19) {
  254.     error("strspn error 4\n");
  255.     }
  256.  
  257.     /*
  258.      * strcspn
  259.      */
  260.  
  261.     strcpy(test, "Now is the time for");
  262.     if (strcspn(test, " ") != 3) {
  263.     error("strcspn error 1\n");
  264.     }
  265.     if (strcspn(test, "N") != 0) {
  266.     error("strcspn error 2\n");
  267.     }
  268.     if (strcspn(test, "") != 19) {
  269.     error("strcspn error 3\n");
  270.     }
  271.     if (strcspn(test, "ABC;") != 19) {
  272.     error("strcspn error 4\n");
  273.     }
  274.  
  275.     return result;
  276. }
  277.