1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-11-04 08:17:17 -05:00
elinks/src/intl/test/utf8-step-test.c
Witold Filipczyk 0fea79cc8f [cflags] Removed -Wno-pointer-sign
Likely some new bugs were introduced by this change.
The long term goal is clean compilation by g++.
2021-01-02 16:20:27 +01: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;
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;
}