summaryrefslogtreecommitdiffabout
path: root/tests/wsbatch.c
blob: fc6439f5764d2839bac96c362ec682e5b286f1b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* wordsplit - a word splitter
   Copyright (C) 2009-2011, 2017 Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3 of the License, or (at your
   option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with this program. If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wordsplit.h>

size_t errors = 0;
size_t count = 0;

void
runtest (const char **input, struct wordsplit *wsinit, int flags)
{
  int i;
  
  for (i = 0; input[i]; i++)
    {
      size_t j;
      const char *text = input[i];
      struct wordsplit ws = *wsinit;
      i++;
      for (j = 0; input[i+j]; j++);

      count++;
      if (wordsplit (text, &ws, flags))
	{
	  fprintf (stderr, "E: %s: parse failed\n", text);
	  errors++;
	}
      else if (ws.ws_wordc != j)
	{
	  fprintf (stderr, "E: %s: wrong number of fields (%lu/%lu)\n",
		   text,
		   (unsigned long) ws.ws_wordc, (unsigned long) j);
	  errors++;
	}
      else
	{
	  size_t k;

	  for (k = 0; k < ws.ws_wordc; k++)
	    if (strcmp (ws.ws_wordv[k], input[i + k]))
	      {
		fprintf (stderr, "E: %s: word %lu mismatch: %s/%s\n",
			 text, (unsigned long) k,
			 ws.ws_wordv[k], input[i + k]);
		errors++;
		break;
	      }
	}
      i += j;
    }
}

const char *testv[] = {
  "a bcd",                       "a", "bcd", NULL,
  "a bcd \tef 13     456",       "a", "bcd", "ef", "13", "456", NULL,
  "a \"s p a c e\" b",           "a", "s p a c e", "b", NULL,
  "a \"s p \\\" a \\\\ c e\" b", "a", "s p \" a \\ c e", "b", NULL,
  "a\\tb \"a\\tb\" 'a\\tb'",     "a\tb", "a\tb", "a\\tb", NULL,
  "\\\\\\a\\b\\f\\n\\r\\t\\v",   "\\\a\b\f\n\r\t\v", NULL,
  "a w13ord #comment",           "a", "w13ord", "#comment", NULL,
  NULL
};

const char *testcmt[] = {
  "a bcd",                       "a", "bcd", NULL,
  "a w13ord #comment",           "a", "w13ord", NULL,
  "#comment",                    NULL,
  NULL
};

const char *testcol[] = {
  "gray:x:1000:100:Sergey Poznyakoff:/home/gray:/bin/bash",
  "gray",
  "x",
  "1000",
  "100",
  "Sergey Poznyakoff",
  "/home/gray",
  "/bin/bash",
  NULL,

  "mail::8:12:\"mail pseudo user\":/:\n",
  "mail",
  "",
  "8",
  "12",
  "\"mail pseudo user\"",
  "/",
  "",
  NULL,

  NULL
};

const char *testcol2[] = {
  "set x=foo,y=bar",
  "set", " ",
  "x", "=",
  "foo", ",",
  "y", "=", "bar",
  NULL,

  NULL
};
  
int
main ()
{
  struct wordsplit ws;

  runtest (testv, &ws, WRDSF_DEFFLAGS);

  ws.ws_comment = "#";
  runtest (testcmt, &ws, WRDSF_DEFFLAGS|WRDSF_COMMENT);

  ws.ws_delim = ":\n";
  runtest (testcol, &ws, WRDSF_NOVAR | WRDSF_NOCMD | WRDSF_DELIM);

  ws.ws_delim = " =,";
  runtest (testcol2, &ws,
	   WRDSF_NOVAR | WRDSF_NOCMD | WRDSF_DELIM | WRDSF_RETURN_DELIMS);

  printf ("Tests: %lu\n", (unsigned long) count);
  if (errors)
    printf ("Failures: %lu\n", (unsigned long) errors);
  return errors ? 1 : 0;
}
  

Return to:

Send suggestions and report system problems to the System administrator.