Home | History | Annotate | Download | only in gentest
      1 /*
      2 *******************************************************************************
      3 *
      4 *   Copyright (C) 1999-2009, International Business Machines
      5 *   Corporation and others.  All Rights Reserved.
      6 *
      7 *******************************************************************************
      8 *   file name:  gentest.c
      9 *   encoding:   US-ASCII
     10 *   tab size:   8 (not used)
     11 *   indentation:4
     12 *
     13 *   created on: 2000mar03
     14 *   created by: Madhu Katragadda
     15 *
     16 *   This program writes a little data file for testing the udata API.
     17 */
     18 
     19 #include <stdio.h>
     20 #include <stdlib.h>
     21 #include "unicode/utypes.h"
     22 #include "unicode/putil.h"
     23 #include "unicode/uclean.h"
     24 #include "unicode/udata.h"
     25 #include "unicode/udbgutil.h"
     26 #include "unewdata.h"
     27 #include "cmemory.h"
     28 #include "cstring.h"
     29 #include "uoptions.h"
     30 #include "gentest.h"
     31 #include "toolutil.h"
     32 
     33 #define DATA_NAME "test"
     34 #define DATA_TYPE "icu"
     35 
     36 /* UDataInfo cf. udata.h */
     37 static const UDataInfo dataInfo={
     38     sizeof(UDataInfo),
     39     0,
     40 
     41     U_IS_BIG_ENDIAN,
     42     U_CHARSET_FAMILY,
     43     sizeof(UChar),
     44     0,
     45 
     46     {0x54, 0x65, 0x73, 0x74},     /* dataFormat="Test" */
     47     {1, 0, 0, 0},                 /* formatVersion */
     48     {1, 0, 0, 0}                  /* dataVersion */
     49 };
     50 
     51 static void createData(const char*, UErrorCode *);
     52 
     53 static int outputJavaStuff(const char * progname, const char *outputDir);
     54 
     55 static UOption options[]={
     56   /*0*/ UOPTION_HELP_H,
     57   /*1*/ UOPTION_HELP_QUESTION_MARK,
     58   /*2*/ UOPTION_DESTDIR,
     59   /*3*/ UOPTION_DEF("genres", 'r', UOPT_NO_ARG),
     60   /*4*/ UOPTION_DEF("javastuff", 'j', UOPT_NO_ARG),
     61 };
     62 
     63 extern int
     64 main(int argc, char* argv[]) {
     65     UErrorCode errorCode = U_ZERO_ERROR;
     66 
     67     /* preset then read command line options */
     68     options[2].value=u_getDataDirectory();
     69     argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options);
     70 
     71     /* error handling, printing usage message */
     72     if(argc<0) {
     73         fprintf(stderr,
     74             "error in command line argument \"%s\"\n",
     75             argv[-argc]);
     76     }
     77     if(argc<0 || options[0].doesOccur || options[1].doesOccur) {
     78         fprintf(stderr,
     79             "usage: %s [-options]\n"
     80             "\tcreate the test file " DATA_NAME "." DATA_TYPE " unless the -r option is given.\n"
     81             "\toptions:\n"
     82             "\t\t-h or -? or --help  this usage text\n"
     83             "\t\t-d or --destdir     destination directory, followed by the path\n"
     84             "\t\t-r or --genres      generate resource file testtable32.txt instead of UData test \n"
     85             "\t\t-j or --javastuff   generate Java source for DebugUtilities. \n",
     86             argv[0]);
     87         return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR;
     88     }
     89 
     90     if( options[4].doesOccur ) {
     91     	return outputJavaStuff( argv[0], options[2].value );
     92     } else if ( options[3].doesOccur ) {
     93         return genres32( argv[0], options[2].value );
     94     } else {
     95         /* printf("Generating the test memory mapped file\n"); */
     96         createData(options[2].value, &errorCode);
     97     }
     98     return U_FAILURE(errorCode);
     99 }
    100 
    101 /* Create data file ----------------------------------------------------- */
    102 static void
    103 createData(const char* outputDirectory, UErrorCode *errorCode) {
    104     UNewDataMemory *pData;
    105     char stringValue[]={'Y', 'E', 'A', 'R', '\0'};
    106     uint16_t intValue=2000;
    107 
    108     long dataLength;
    109     uint32_t size;
    110 
    111     pData=udata_create(outputDirectory, DATA_TYPE, DATA_NAME, &dataInfo,
    112                        U_COPYRIGHT_STRING, errorCode);
    113     if(U_FAILURE(*errorCode)) {
    114         fprintf(stderr, "gentest: unable to create data memory, %s\n", u_errorName(*errorCode));
    115         exit(*errorCode);
    116     }
    117 
    118     /* write the data to the file */
    119     /* a 16 bit value  and a String*/
    120     udata_write16(pData, intValue);
    121     udata_writeString(pData, stringValue, sizeof(stringValue));
    122 
    123     /* finish up */
    124     dataLength=udata_finish(pData, errorCode);
    125     if(U_FAILURE(*errorCode)) {
    126         fprintf(stderr, "gentest: error %d writing the output file\n", *errorCode);
    127         exit(*errorCode);
    128     }
    129     size=sizeof(stringValue) + sizeof(intValue);
    130 
    131 
    132     if(dataLength!=(long)size) {
    133         fprintf(stderr, "gentest: data length %ld != calculated size %lu\n",
    134             dataLength, (unsigned long)size);
    135         exit(U_INTERNAL_PROGRAM_ERROR);
    136     }
    137 }
    138 
    139 /* Create Java file ----------------------------------------------------- */
    140 
    141 static int
    142 outputJavaStuff(const char* progname, const char *outputDir) {
    143     int32_t i,t,count;
    144     char file[512];
    145     FILE *out;
    146     int32_t year = getCurrentYear();
    147 
    148     uprv_strcpy(file,outputDir);
    149     if(*outputDir &&  /* don't put a trailing slash if outputDir is empty */
    150         file[strlen(file)-1]!=U_FILE_SEP_CHAR) {
    151             uprv_strcat(file,U_FILE_SEP_STRING);
    152     }
    153     uprv_strcat(file,"DebugUtilitiesData.java");
    154     out = fopen(file, "w");
    155     /*puts(file);*/
    156     printf("%s: Generating %s\n", progname, file);
    157     if(out == NULL) {
    158         fprintf(stderr, "%s: Couldn't create resource test file %s\n",
    159             progname, file);
    160         return 1;
    161     }
    162 
    163     fprintf(out, "/** Copyright (C) %d, International Business Machines Corporation and Others. All Rights Reserved. **/\n\n", year);
    164     fprintf(out, "/* NOTE: this file is AUTOMATICALLY GENERATED by gentest. */\n\n");
    165     fprintf(out, "package com.ibm.icu.dev.test.util;\n\n");
    166     fprintf(out, "public class DebugUtilitiesData extends Object {\n");
    167     fprintf(out, "    public static final String ICU4C_VERSION=\"%s\";\n", U_ICU_VERSION);
    168     for(t=0;t<UDBG_ENUM_COUNT;t++) {
    169         fprintf(out, "    public static final int %s = %d;\n", udbg_enumName(UDBG_UDebugEnumType,t), t);
    170     }
    171     fprintf(out, "    public static final String [] TYPES = { \n");
    172     for(t=0;t<UDBG_ENUM_COUNT;t++) {
    173         fprintf(out, "        \"%s\", /* %d */\n", udbg_enumName(UDBG_UDebugEnumType,t), t);
    174     }
    175     fprintf(out, "    };\n\n");
    176 
    177     fprintf(out, "    public static final String [][] NAMES = { \n");
    178     for(t=0;t<UDBG_ENUM_COUNT;t++) {
    179         count = udbg_enumCount((UDebugEnumType)t);
    180         fprintf(out, "        /* %s, %d */\n", udbg_enumName(UDBG_UDebugEnumType,t), t);
    181         fprintf(out, "        { \n");
    182         for(i=0;i<count;i++) {
    183             fprintf(out,
    184                 "           \"%s\", /* %d */ \n", udbg_enumName((UDebugEnumType)t,i), i);
    185         }
    186         fprintf(out, "        },\n");
    187     }
    188     fprintf(out, "    };\n\n");
    189 
    190     fprintf(out, "    public static final int [][] VALUES = { \n");
    191     for(t=0;t<UDBG_ENUM_COUNT;t++) {
    192         count = udbg_enumCount((UDebugEnumType)t);
    193         fprintf(out, "        /* %s, %d */\n", udbg_enumName(UDBG_UDebugEnumType,t), t);
    194         fprintf(out, "        { \n");
    195         for(i=0;i<count;i++) {
    196             fprintf(out,
    197                 "           ");
    198             switch(t) {
    199             case UDBG_UCalendarDateFields:
    200             case UDBG_UCalendarMonths:
    201                 /* Temporary workaround for IS_LEAP_MOTH #6051 */
    202                 if (t == UDBG_UCalendarDateFields && i == 22) {
    203                     fprintf(out, "com.ibm.icu.util.ChineseCalendar.%s, /* %d */", udbg_enumName((UDebugEnumType)t,i), i);
    204                 } else
    205                 fprintf(out, "com.ibm.icu.util.Calendar.%s, /* %d */", udbg_enumName((UDebugEnumType)t,i), i);
    206                 break;
    207             case UDBG_UDebugEnumType:
    208             default:
    209                 fprintf(out,"%d, /* %s */", i, udbg_enumName((UDebugEnumType)t,i));
    210             }
    211             fprintf(out,"\n");
    212         }
    213         fprintf(out, "        },\n");
    214     }
    215     fprintf(out, "    };\n\n");
    216     fprintf(out, "}\n");
    217 
    218     fclose(out);
    219 
    220     return 0;
    221 
    222 #if 0
    223     int32_t count = udbg_enumCount((UDebugEnumType)t);
    224     if(count == -1) {
    225         fprintf(stderr,"%s: enumCount(%d) returned -1\n", progname, count);
    226         return 1;
    227     }
    228     for(t=0;t<=UDBG_ENUM_COUNT;t++) {
    229         int32_t count = udbg_enumCount((UDebugEnumType)t);
    230         if(count == -1) {
    231             fprintf(stderr,"%s: enumCount(%d) returned -1\n", progname, count);
    232             return 1;
    233         }
    234         for(i=0;i<=count;i++) {
    235             if(i<count) {
    236                 if( i!=udbg_enumArrayValue((UDebugEnumType)t, i)) {
    237                     fprintf(stderr, "%s: FAIL: udbg_enumArrayValue(%d,%d) returned %d, expected %d\n", progname, t, i, udbg_enumArrayValue((UDebugEnumType)t,i), i);
    238                     return 1;
    239                 }
    240             }
    241             fprintf(stderr, "%s: udbg_enumArrayValue(%d,%d) = %s, returned %d\n", progname, t, i,
    242                 udbg_enumName((UDebugEnumType)t,i), udbg_enumArrayValue((UDebugEnumType)t,i));
    243         }
    244         if(udbg_enumExpectedCount((UDebugEnumType)t) != count) {
    245             fprintf(stderr, "%s: FAIL: udbg_enumExpectedCount(%d): %d, != UCAL_FIELD_COUNT=%d \n", progname, t, udbg_enumExpectedCount((UDebugEnumType)t), count);
    246             return 1;
    247         } else {
    248             fprintf(stderr, "%s: udbg_ucal_fieldCount: %d, UCAL_FIELD_COUNT=udbg_enumCount %d ", progname, udbg_enumExpectedCount((UDebugEnumType)t), count);
    249         }
    250     }
    251 #endif
    252 }
    253