]> git.ipfire.org Git - thirdparty/cups.git/commitdiff
Fix _cupsArrayAdd/NewStrings implementation with spaces. Also add unit test
authormike <mike@7a7537e8-13f0-0310-91df-b6672ffda945>
Tue, 23 Oct 2012 18:28:49 +0000 (18:28 +0000)
committermike <mike@7a7537e8-13f0-0310-91df-b6672ffda945>
Tue, 23 Oct 2012 18:28:49 +0000 (18:28 +0000)
for these functions.

git-svn-id: svn+ssh://src.apple.com/svn/cups/cups.org/trunk@10659 7a7537e8-13f0-0310-91df-b6672ffda945

cups/array.c
cups/testarray.c

index ed8d2c642d44cb004bbe5ea24282a1988a664827..9fbba6205ed16d6e6351bdb07277485978a629ad 100644 (file)
@@ -156,8 +156,14 @@ _cupsArrayAddStrings(cups_array_t *a,      /* I - Array */
   int          status = 1;             /* Status of add */
 
 
+  DEBUG_printf(("_cupsArrayAddStrings(a=%p, s=\"%s\", delim='%c')", a, s,
+                delim));
+
   if (!a || !s || !*s)
+  {
+    DEBUG_puts("1_cupsArrayAddStrings: Returning 0");
     return (0);
+  }
 
   if (delim == ' ')
   {
@@ -165,22 +171,32 @@ _cupsArrayAddStrings(cups_array_t *a,     /* I - Array */
     * Skip leading whitespace...
     */
 
+    DEBUG_puts("1_cupsArrayAddStrings: Skipping leading whitespace.");
+
     while (*s && isspace(*s & 255))
       s ++;
+
+    DEBUG_printf(("1_cupsArrayAddStrings: Remaining string \"%s\".", s));
   }
 
-  if (!strchr(s, delim) ||
-      (delim == ' ' && !strchr(s, '\t') && !strchr(s, '\n')))
+  if (!strchr(s, delim) &&
+      (delim != ' ' || (!strchr(s, '\t') && !strchr(s, '\n'))))
   {
    /*
     * String doesn't contain a delimiter, so add it as a single value...
     */
 
+    DEBUG_puts("1_cupsArrayAddStrings: No delimiter seen, adding a single "
+               "value.");
+
     if (!cupsArrayFind(a, (void *)s))
       status = cupsArrayAdd(a, (void *)s);
   }
   else if ((buffer = strdup(s)) == NULL)
+  {
+    DEBUG_puts("1_cupsArrayAddStrings: Unable to duplicate string.");
     status = 0;
+  }
   else
   {
     for (start = end = buffer; *end; start = end)
@@ -202,6 +218,9 @@ _cupsArrayAddStrings(cups_array_t *a,       /* I - Array */
       else
         end = start + strlen(start);
 
+      DEBUG_printf(("1_cupsArrayAddStrings: Adding \"%s\", end=\"%s\"", start,
+                    end));
+
       if (!cupsArrayFind(a, start))
         status &= cupsArrayAdd(a, start);
     }
@@ -209,6 +228,8 @@ _cupsArrayAddStrings(cups_array_t *a,       /* I - Array */
     free(buffer);
   }
 
+  DEBUG_printf(("1_cupsArrayAddStrings: Returning %d.", status));
+
   return (status);
 }
 
index cdae3c7a54b5468f7b5734fb09bb434ff645a8e5..1f9af5ef1ff3fa69acc75d454eec2a8ca225b709 100644 (file)
@@ -27,7 +27,7 @@
 
 #include "string-private.h"
 #include "debug-private.h"
-#include "array.h"
+#include "array-private.h"
 #include "dir.h"
 
 
@@ -404,6 +404,68 @@ main(int  argc,                            /* I - Number of command-line arguments */
   cupsArrayDelete(array);
   cupsArrayDelete(dup_array);
 
+ /*
+  * Test the array with string functions...
+  */
+
+  fputs("_cupsArrayNewStrings(\"foo bar\", ' '): ", stdout);
+  array = _cupsArrayNewStrings("foo bar", ' ');
+  if (!array)
+  {
+    status = 1;
+    puts("FAIL (unable to create array)");
+  }
+  else if (cupsArrayCount(array) != 2)
+  {
+    status = 1;
+    printf("FAIL (got %d elements, expected 2)\n", cupsArrayCount(array));
+  }
+  else if (strcmp(text = (char *)cupsArrayFirst(array), "bar"))
+  {
+    status = 1;
+    printf("FAIL (first element \"%s\", expected \"bar\")\n", text);
+  }
+  else if (strcmp(text = (char *)cupsArrayNext(array), "foo"))
+  {
+    status = 1;
+    printf("FAIL (first element \"%s\", expected \"foo\")\n", text);
+  }
+  else
+    puts("PASS");
+
+  fputs("_cupsArrayAddStrings(array, \"foo2,bar2\", ','): ", stdout);
+  _cupsArrayAddStrings(array, "foo2,bar2", ',');
+
+  if (cupsArrayCount(array) != 4)
+  {
+    status = 1;
+    printf("FAIL (got %d elements, expected 4)\n", cupsArrayCount(array));
+  }
+  else if (strcmp(text = (char *)cupsArrayFirst(array), "bar"))
+  {
+    status = 1;
+    printf("FAIL (first element \"%s\", expected \"bar\")\n", text);
+  }
+  else if (strcmp(text = (char *)cupsArrayNext(array), "bar2"))
+  {
+    status = 1;
+    printf("FAIL (first element \"%s\", expected \"bar2\")\n", text);
+  }
+  else if (strcmp(text = (char *)cupsArrayNext(array), "foo"))
+  {
+    status = 1;
+    printf("FAIL (first element \"%s\", expected \"foo\")\n", text);
+  }
+  else if (strcmp(text = (char *)cupsArrayNext(array), "foo2"))
+  {
+    status = 1;
+    printf("FAIL (first element \"%s\", expected \"foo2\")\n", text);
+  }
+  else
+    puts("PASS");
+
+  cupsArrayDelete(array);
+
  /*
   * Summarize the results and return...
   */