File: utf8_test.c

package info (click to toggle)
bibutils 7.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,040 kB
  • sloc: ansic: 112,579; sh: 462; makefile: 42
file content (57 lines) | stat: -rw-r--r-- 1,067 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
 * utf8_test.c
 *
 * Copyright (c) 2012-2021
 *
 * Source code released under the GPL version 2
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include "utf8.h"

char progname[] = "utf8_test";

int
test_utf8( void )
{
	unsigned char ubuf[512];
	char buf[512];
	unsigned int i, j, pos_out;
	int nc, pos, failed = 0;
	for ( i=0; i<1000000; ++i ) {
		nc = utf8_encode( i, ubuf );
		ubuf[ nc ] = '*';
		ubuf[ nc+1 ] = '\0';
		for ( pos=0; pos < nc+2; ++pos )
			buf[pos] = (char)ubuf[pos];
		pos_out = 0;
		j = utf8_decode( buf, &pos_out );
		if ( i != j ) {
			printf( "%s: Error test_utf8 mismatch, "
				"send %u got back %u\n", progname, i, j );
			failed = 1;
		}
		if ( buf[pos_out]!='*' ) {
			printf( "%s: Error test_utf8 bad ending pos, "
				"expect '*', got back '%c'\n", progname,
				buf[pos] );
		}
	}
	return failed;
}


int
main( int argc, char *argv[] )
{
	int failed = 0;
	failed += test_utf8();
	if ( !failed ) {
		printf( "%s: PASSED\n", progname );
		return EXIT_SUCCESS;
	} else {
		printf( "%s: FAILED\n", progname );
		return EXIT_FAILURE;
	}
}