| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,137 @@ |
| 1 |
+/* |
|
| 2 |
+ * re_tests_utf8.c |
|
| 3 |
+ * |
|
| 4 |
+ * A programmers editor |
|
| 5 |
+ * |
|
| 6 |
+ * Tests (ensure correct functionality of modules) |
|
| 7 |
+ * |
|
| 8 |
+ * Author: Dario Rodriguez dario@softhome.net |
|
| 9 |
+ * This program is licensed under the terms of GNU GPL v2.1+ |
|
| 10 |
+ */ |
|
| 11 |
+ |
|
| 12 |
+#include <stdio.h> |
|
| 13 |
+#include <stdlib.h> |
|
| 14 |
+#include <unistd.h> |
|
| 15 |
+#include <string.h> |
|
| 16 |
+#include <limits.h> |
|
| 17 |
+#include <fcntl.h> |
|
| 18 |
+#include <errno.h> |
|
| 19 |
+ |
|
| 20 |
+#include "re_data.h" |
|
| 21 |
+ |
|
| 22 |
+#define TEST_OK "OK" |
|
| 23 |
+ |
|
| 24 |
+typedef struct test_ui_t {
|
|
| 25 |
+ char *name; |
|
| 26 |
+ char *(*fn)(char *,int , int, int, int); |
|
| 27 |
+} test_ui_t; |
|
| 28 |
+ |
|
| 29 |
+char *test_utf8len(char *teststring, int expectednchars, int dummy1, int dummy2,int dummy3); |
|
| 30 |
+char *test_utf8col(char *teststring, int dummy0, int col, int expectedoffset,int dummy3); |
|
| 31 |
+char *test_utf8charlen(char *teststring, int dummy0, int dummy1, int offset,int charsizeatoffset); |
|
| 32 |
+ |
|
| 33 |
+int |
|
| 34 |
+main(int argc, char *argv[]) |
|
| 35 |
+{
|
|
| 36 |
+ struct {
|
|
| 37 |
+ char string[1024]; |
|
| 38 |
+ int nchars; |
|
| 39 |
+ int col; |
|
| 40 |
+ int offsetcol; |
|
| 41 |
+ int charsizeatoffsetcol; |
|
| 42 |
+ } teststrings[]={
|
|
| 43 |
+ {{"This is a latin1 string"},51-28,1,1,1},
|
|
| 44 |
+ {{"lowercase acute vowels:\xc3\xa1\xc3\xa9\xc3\xad\xc3\xb3\xc3\xba"},23+5,23+1,23+2,2},
|
|
| 45 |
+ }; |
|
| 46 |
+ test_ui_t tests[]={
|
|
| 47 |
+ {"utf8len",test_utf8len},
|
|
| 48 |
+ {"utf8col",test_utf8col},
|
|
| 49 |
+ {"utf8charlen",test_utf8charlen},
|
|
| 50 |
+ }; |
|
| 51 |
+ int flag_exit=0,flag_all=0; |
|
| 52 |
+ int i,s; |
|
| 53 |
+ int nerrors,total; |
|
| 54 |
+ char *res; |
|
| 55 |
+ for(i=1;i<argc;i++) {
|
|
| 56 |
+ if(strcmp(argv[i],"--help")==0) {
|
|
| 57 |
+ fprintf(stderr,"Syntax: %s [--all] [--exit] [--help]\nExplanation:\n\t--all: do even the slow tests\n\t--exit: exit program at first unsuccessful test\n\t--help: this text\n",argv[0]); |
|
| 58 |
+ return(1); |
|
| 59 |
+ } else if(strcmp(argv[i],"--all")==0) {
|
|
| 60 |
+ flag_all=1; |
|
| 61 |
+ } else if(strcmp(argv[i],"--exit")==0) {
|
|
| 62 |
+ flag_exit=1; |
|
| 63 |
+ } |
|
| 64 |
+ } |
|
| 65 |
+ nerrors=0; |
|
| 66 |
+ total=0; |
|
| 67 |
+ /* flag_all is not used right now, next line is to silence the compiler */ |
|
| 68 |
+ total+=(flag_all&0); |
|
| 69 |
+ /* end of hack */ |
|
| 70 |
+ for(s=0;s<(sizeof(teststrings)/sizeof(teststrings[0]));s++) {
|
|
| 71 |
+ fprintf(stderr,"\"%s\"\n",teststrings[s].string); |
|
| 72 |
+ for(i=0;i<(sizeof(tests)/sizeof(tests[0]));i++,total++) {
|
|
| 73 |
+ fprintf(stderr,"%i:%s...",total+1,tests[i].name); |
|
| 74 |
+ res=tests[i].fn(teststrings[s].string,teststrings[s].nchars,teststrings[s].col,teststrings[s].offsetcol,teststrings[s].charsizeatoffsetcol); |
|
| 75 |
+ if(strcmp(res,TEST_OK)==0) {
|
|
| 76 |
+ fprintf(stderr," ok.\n"); |
|
| 77 |
+ } else {
|
|
| 78 |
+ fprintf(stderr," ERROR: %s <= %s(\"%s\",%i,%i,%i,%i)\n",res,tests[i].name,teststrings[s].string,teststrings[s].nchars,teststrings[s].col,teststrings[s].offsetcol,teststrings[s].charsizeatoffsetcol); |
|
| 79 |
+ nerrors++; |
|
| 80 |
+ if(flag_exit) {
|
|
| 81 |
+ /* exit on first error */ |
|
| 82 |
+ s=sizeof(teststrings)/sizeof(teststrings[0]); |
|
| 83 |
+ break; |
|
| 84 |
+ } |
|
| 85 |
+ } |
|
| 86 |
+ } |
|
| 87 |
+ } |
|
| 88 |
+ fprintf(stderr,"\n"); |
|
| 89 |
+ if(nerrors==0) |
|
| 90 |
+ fprintf(stderr,"All %i tests passed OK\n",total); |
|
| 91 |
+ else |
|
| 92 |
+ fprintf(stderr,"%i test(s) failed of %i tests run.\n",nerrors,total); |
|
| 93 |
+ return((nerrors==0)?0:1); |
|
| 94 |
+} |
|
| 95 |
+ |
|
| 96 |
+char * |
|
| 97 |
+test_utf8len(char *teststring, int expectednchars, int dummy1, int dummy2, int dummy3) |
|
| 98 |
+{
|
|
| 99 |
+ int res; |
|
| 100 |
+ static char errorstr[1024]; |
|
| 101 |
+ res=redata_generic_utf8len(teststring,strlen(teststring)); |
|
| 102 |
+ if(res!=expectednchars) {
|
|
| 103 |
+ snprintf(errorstr,sizeof(errorstr),"expected %i chars, got %i chars",expectednchars,res); |
|
| 104 |
+ errorstr[sizeof(errorstr)-1]='\0'; |
|
| 105 |
+ return(errorstr); |
|
| 106 |
+ } |
|
| 107 |
+ return(TEST_OK); |
|
| 108 |
+} |
|
| 109 |
+ |
|
| 110 |
+char *test_utf8col(char *teststring, int dummy0, int col, int expectedoffset, int dummy3) |
|
| 111 |
+{
|
|
| 112 |
+ char *ptr; |
|
| 113 |
+ static char errorstr[1024]; |
|
| 114 |
+ ptr=redata_generic_utf8col(teststring,strlen(teststring),col); |
|
| 115 |
+ if(ptr!=(teststring+expectedoffset)) {
|
|
| 116 |
+ snprintf(errorstr,sizeof(errorstr),"expected offset %i, got offset %i (\"%s\")",expectedoffset,(int) ((ptr==NULL)?-1:ptr-teststring),ptr); |
|
| 117 |
+ errorstr[sizeof(errorstr)-1]='\0'; |
|
| 118 |
+ return(errorstr); |
|
| 119 |
+ } |
|
| 120 |
+ return(TEST_OK); |
|
| 121 |
+} |
|
| 122 |
+ |
|
| 123 |
+char * |
|
| 124 |
+test_utf8charlen(char *teststring, int dummy0, int dummy1, int offset,int charsizeatoffset) |
|
| 125 |
+{
|
|
| 126 |
+ int res; |
|
| 127 |
+ static char errorstr[1024]; |
|
| 128 |
+ res=redata_generic_utf8charlen(teststring+offset,strlen(teststring+offset)); |
|
| 129 |
+ if(res!=(charsizeatoffset)) {
|
|
| 130 |
+ snprintf(errorstr,sizeof(errorstr),"expected char size %i, got char size %i (\"%s\")",charsizeatoffset,res,teststring+offset); |
|
| 131 |
+ errorstr[sizeof(errorstr)-1]='\0'; |
|
| 132 |
+ return(errorstr); |
|
| 133 |
+ } |
|
| 134 |
+ return(TEST_OK); |
|
| 135 |
+} |
|
| 136 |
+ |
|
| 137 |
+ |