File: test_wcwidth.c

package info (click to toggle)
libtsm 4.1.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 948 kB
  • sloc: ansic: 13,275; makefile: 32
file content (43 lines) | stat: -rw-r--r-- 1,043 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>

#include "wcwidth.h"

static int tests_run;
static int test_failures;

void assertWidthIs(int expected_width, wchar_t c) {
	tests_run++;
        int actual_width = wcwidth(c);
        if (actual_width != expected_width) {
                fprintf(stderr, "ERROR: wcwidth(U+%04x) returned %d, expected %d\n", c, actual_width, expected_width);
		test_failures++;
        }
}

int main() {
        assertWidthIs(1, 'a');
        assertWidthIs(1, L'ö');

	// Some wide:
	assertWidthIs(2, L'A');
	assertWidthIs(2, L'B');
	assertWidthIs(2, L'C');
	assertWidthIs(2, L'中');
	assertWidthIs(2, L'文');
	assertWidthIs(2, 0x679C);
	assertWidthIs(2, 0x679D);
	assertWidthIs(2, 0x2070E);
	assertWidthIs(2, 0x20731);

	assertWidthIs(1, 0x11A3);

	assertWidthIs(2, 0x1F428); // Koala emoji.
        assertWidthIs(2, 0x231a);  // Watch emoji.

	if (test_failures > 0) printf("%d tests FAILED, ", test_failures);
	printf("%d tests OK\n", tests_run);
	return (test_failures == 0) ? 0 : 1;
}