iconv: Bail out of the loop when an illegal sequence of bytes occurs.
[elinks/elinks-j605.git] / src / intl / test / utf8-step-test.c
blob0276c47cad6037cc6f3d93f838c67561845074ad
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
5 #include <stdarg.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
10 #include "elinks.h"
12 #include "intl/charsets.h"
13 #include "util/test.h"
15 enum direction {
16 DIRECTION_FORWARD,
17 DIRECTION_BACKWARD
20 static enum direction
21 parse_direction(const char *name)
23 if (strcmp(name, "forward") == 0)
24 return DIRECTION_FORWARD;
25 else if (strcmp(name, "backward") == 0)
26 return DIRECTION_BACKWARD;
27 else
28 die("direction must be forward or backward, not %s\n", name);
31 static enum utf8_step
32 parse_way(const char *name)
34 if (strcmp(name, "characters") == 0)
35 return UTF8_STEP_CHARACTERS;
36 else if (strcmp(name, "cells-fewer") == 0)
37 return UTF8_STEP_CELLS_FEWER;
38 else if (strcmp(name, "cells-more") == 0)
39 return UTF8_STEP_CELLS_MORE;
40 else
41 die("way must be characters, cells-fewer, or cells-more; not %s\n", name);
44 int
45 main(int argc, char **argv)
47 enum direction direction;
48 enum utf8_step way;
49 int max_steps;
50 unsigned char *begin, *end;
51 int expect_bytes;
52 int expect_steps;
53 int bytes;
54 int steps;
56 if (argc != 7)
57 die("usage: %s (forward|backward) (characters|cells-fewer|cells-more) \\\n"
58 "\tMAX-STEPS STRING EXPECT-BYTES EXPECT-STEPS\n",
59 argc ? argv[0] : "utf8-step-test");
61 direction = parse_direction(argv[1]);
62 way = parse_way(argv[2]);
63 max_steps = atoi(argv[3]);
64 begin = argv[4];
65 end = begin + strlen(begin);
66 expect_bytes = atoi(argv[5]);
67 expect_steps = atoi(argv[6]);
69 switch (direction) {
70 case DIRECTION_FORWARD:
71 bytes = utf8_step_forward(begin, end, max_steps, way, &steps) - begin;
72 break;
73 case DIRECTION_BACKWARD:
74 bytes = end - utf8_step_backward(end, begin, max_steps, way, &steps);
75 break;
76 default:
77 abort();
80 if (bytes != expect_bytes || steps != expect_steps)
81 die("FAIL: bytes=%d (expected %d); steps=%d (expected %d)\n",
82 bytes, expect_bytes, steps, expect_steps);
84 return 0;