12 #include "intl/charsets.h"
13 #include "util/test.h"
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
;
28 die("direction must be forward or backward, not %s\n", name
);
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
;
41 die("way must be characters, cells-fewer, or cells-more; not %s\n", name
);
45 main(int argc
, char **argv
)
47 enum direction direction
;
50 unsigned char *begin
, *end
;
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]);
65 end
= begin
+ strlen(begin
);
66 expect_bytes
= atoi(argv
[5]);
67 expect_steps
= atoi(argv
[6]);
70 case DIRECTION_FORWARD
:
71 bytes
= utf8_step_forward(begin
, end
, max_steps
, way
, &steps
) - begin
;
73 case DIRECTION_BACKWARD
:
74 bytes
= end
- utf8_step_backward(end
, begin
, max_steps
, way
, &steps
);
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
);