1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-07-22 16:04:24 -04:00
elinks/src/intl/test/utf8-step-test.c
Kalle Olavi Niemitalo b6aca8d9a7 Add tests for utf8_step_forward
I am not hooking these to "make test", for two reasons:

1. utf8_step_forward is inside #ifdef CONFIG_UTF8 and I don't see
   how to make tests conditional on such options.

2. test/libtest.sh was copied from Git, which is under GPLv2-only.
   Adding more dependencies on it could make ELinks more difficult
   to relicense under GPLv2-or-later.
2009-05-27 01:11:03 +03:00

86 lines
1.9 KiB
C

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "elinks.h"
#include "intl/charsets.h"
#include "util/test.h"
enum direction {
DIRECTION_FORWARD,
DIRECTION_BACKWARD
};
static enum direction
parse_direction(const char *name)
{
if (strcmp(name, "forward") == 0)
return DIRECTION_FORWARD;
else if (strcmp(name, "backward") == 0)
return DIRECTION_BACKWARD;
else
die("direction must be forward or backward, not %s\n", name);
}
static enum utf8_step
parse_way(const char *name)
{
if (strcmp(name, "characters") == 0)
return UTF8_STEP_CHARACTERS;
else if (strcmp(name, "cells-fewer") == 0)
return UTF8_STEP_CELLS_FEWER;
else if (strcmp(name, "cells-more") == 0)
return UTF8_STEP_CELLS_MORE;
else
die("way must be characters, cells-fewer, or cells-more; not %s\n", name);
}
int
main(int argc, char **argv)
{
enum direction direction;
enum utf8_step way;
int max_steps;
unsigned char *begin, *end;
int expect_bytes;
int expect_steps;
int bytes;
int steps;
if (argc != 7)
die("usage: %s (forward|backward) (characters|cells-fewer|cells-more) \\\n"
"\tMAX-STEPS STRING EXPECT-BYTES EXPECT-STEPS\n",
argc ? argv[0] : "utf8-step-test");
direction = parse_direction(argv[1]);
way = parse_way(argv[2]);
max_steps = atoi(argv[3]);
begin = argv[4];
end = begin + strlen(begin);
expect_bytes = atoi(argv[5]);
expect_steps = atoi(argv[6]);
switch (direction) {
case DIRECTION_FORWARD:
bytes = utf8_step_forward(begin, end, max_steps, way, &steps) - begin;
break;
case DIRECTION_BACKWARD:
bytes = end - utf8_step_backward(end, begin, max_steps, way, &steps);
break;
default:
abort();
}
if (bytes != expect_bytes || steps != expect_steps)
die("FAIL: bytes=%d (expected %d); steps=%d (expected %d)\n",
bytes, expect_bytes, steps, expect_steps);
return 0;
}