/* * re_tests_utf8.c * * A programmers editor * * Tests (ensure correct functionality of modules) * * Author: Dario Rodriguez antartica@whereismybit.com * This program is licensed under the terms of GNU GPL v2.1+ */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <limits.h> #include <fcntl.h> #include <errno.h> #include "re_data.h" #define TEST_OK "OK" typedef struct test_ui_t { char *name; char *(*fn)(char *,int , int, int, int); } test_ui_t; char *test_utf8len(char *teststring, int expectednchars, int dummy1, int dummy2,int dummy3); char *test_utf8col(char *teststring, int dummy0, int col, int expectedoffset,int dummy3); char *test_utf8charlen(char *teststring, int dummy0, int dummy1, int offset,int charsizeatoffset); int main(int argc, char *argv[]) { struct { char string[1024]; int nchars; int col; int offsetcol; int charsizeatoffsetcol; } teststrings[]={ {{"This is a latin1 string"},51-28,1,1,1}, {{"lowercase acute vowels:\xc3\xa1\xc3\xa9\xc3\xad\xc3\xb3\xc3\xba"},23+5,23+1,23+2,2}, }; test_ui_t tests[]={ {"utf8len",test_utf8len}, {"utf8col",test_utf8col}, {"utf8charlen",test_utf8charlen}, }; int flag_exit=0,flag_all=0; int i,s; int nerrors,total; char *res; for(i=1;i<argc;i++) { if(strcmp(argv[i],"--help")==0) { 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]); return(1); } else if(strcmp(argv[i],"--all")==0) { flag_all=1; } else if(strcmp(argv[i],"--exit")==0) { flag_exit=1; } } nerrors=0; total=0; /* flag_all is not used right now, next line is to silence the compiler */ total+=(flag_all&0); /* end of hack */ for(s=0;s<(sizeof(teststrings)/sizeof(teststrings[0]));s++) { fprintf(stderr,"\"%s\"\n",teststrings[s].string); for(i=0;i<(sizeof(tests)/sizeof(tests[0]));i++,total++) { fprintf(stderr,"%i:%s...",total+1,tests[i].name); res=tests[i].fn(teststrings[s].string,teststrings[s].nchars,teststrings[s].col,teststrings[s].offsetcol,teststrings[s].charsizeatoffsetcol); if(strcmp(res,TEST_OK)==0) { fprintf(stderr," ok.\n"); } else { 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); nerrors++; if(flag_exit) { /* exit on first error */ s=sizeof(teststrings)/sizeof(teststrings[0]); break; } } } } fprintf(stderr,"\n"); if(nerrors==0) fprintf(stderr,"All %i tests passed OK\n",total); else fprintf(stderr,"%i test(s) failed of %i tests run.\n",nerrors,total); return((nerrors==0)?0:1); } char * test_utf8len(char *teststring, int expectednchars, int dummy1, int dummy2, int dummy3) { int res; static char errorstr[1024]; res=redata_generic_utf8len(teststring,strlen(teststring)); if(res!=expectednchars) { snprintf(errorstr,sizeof(errorstr),"expected %i chars, got %i chars",expectednchars,res); errorstr[sizeof(errorstr)-1]='\0'; return(errorstr); } return(TEST_OK); } char *test_utf8col(char *teststring, int dummy0, int col, int expectedoffset, int dummy3) { char *ptr; static char errorstr[1024]; ptr=redata_generic_utf8col(teststring,strlen(teststring),col); if(ptr!=(teststring+expectedoffset)) { snprintf(errorstr,sizeof(errorstr),"expected offset %i, got offset %i (\"%s\")",expectedoffset,(int) ((ptr==NULL)?-1:ptr-teststring),ptr); errorstr[sizeof(errorstr)-1]='\0'; return(errorstr); } return(TEST_OK); } char * test_utf8charlen(char *teststring, int dummy0, int dummy1, int offset,int charsizeatoffset) { int res; static char errorstr[1024]; res=redata_generic_utf8charlen(teststring+offset,strlen(teststring+offset)); if(res!=(charsizeatoffset)) { snprintf(errorstr,sizeof(errorstr),"expected char size %i, got char size %i (\"%s\")",charsizeatoffset,res,teststring+offset); errorstr[sizeof(errorstr)-1]='\0'; return(errorstr); } return(TEST_OK); }