1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

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.
This commit is contained in:
Kalle Olavi Niemitalo 2009-05-27 00:44:27 +03:00 committed by Kalle Olavi Niemitalo
parent 5aae1b81cc
commit b6aca8d9a7
2 changed files with 96 additions and 0 deletions

View File

@ -0,0 +1,11 @@
#! /bin/sh -e
./utf8-step-test forward cells-more 999 日本人 9 6
./utf8-step-test forward cells-more 6 日本人 9 6
./utf8-step-test forward cells-more 5 日本人 9 6
./utf8-step-test forward cells-more 4 日本人 6 4
./utf8-step-test forward cells-fewer 999 日本人 9 6
./utf8-step-test forward cells-fewer 6 日本人 9 6
./utf8-step-test forward cells-fewer 5 日本人 6 4
./utf8-step-test forward cells-fewer 4 日本人 6 4

View File

@ -0,0 +1,85 @@
#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;
}