home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / cintltst / cdtrgtst.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  14.6 KB  |  475 lines

  1. /*
  2. *****************************************************************************************
  3. *                                                                                       *
  4. * COPYRIGHT:                                                                            *
  5. *   (C) Copyright Taligent, Inc.,  1996                                                 *
  6. *   (C) Copyright International Business Machines Corporation,  1999                    *
  7. *   Licensed Material - Program-Property of IBM - All Rights Reserved.                  *
  8. *   US Government Users Restricted Rights - Use, duplication, or disclosure             *
  9. *   restricted by GSA ADP Schedule Contract with IBM Corp.                              *
  10. *                                                                                       *
  11. *****************************************************************************************
  12. ********************************************************************************
  13. *
  14. * File CDTRGTST.C
  15. *
  16. *     Madhu Katragadda            Ported for C API
  17. * Modification History:
  18. *   Date        Name        Description
  19. *   07/15/99    helena      Ported to HPUX 10/11 CC.
  20. *********************************************************************************
  21. */
  22. /* REGRESSION TEST FOR DATE FORMAT */
  23.  
  24. #include "uloc.h"
  25. #include "utypes.h"
  26. #include "udat.h"
  27. #include "ucal.h"
  28. #include "unum.h"
  29. #include "ustring.h"
  30. #include "cintltst.h"
  31. #include "cdtrgtst.h"
  32. #include<stdio.h>
  33. #include<string.h>
  34.  
  35.  
  36. void addDateForRgrTest(TestNode** root)
  37. {
  38.     addTest(root, &Test4029195, "tsformat/cdtrgtst/Test4029195");
  39.     addTest(root, &Test4056591, "tsformat/cdtrgtst/Test4056591");
  40.     addTest(root, &Test4059917, "tsformat/cdtrgtst/Test4059917");
  41.     addTest(root, &Test4060212, "tsformat/cdtrgtst/Test4060212");
  42.     addTest(root, &Test4061287, "tsformat/cdtrgtst/Test4061287");
  43.     addTest(root, &Test4073003, "tsformat/cdtrgtst/Test4073003");
  44.     addTest(root, &Test4162071, "tsformat/cdtrgtst/Test4162071");
  45.       
  46.             
  47. }
  48.  
  49. /**
  50.  * @bug 4029195
  51.  */
  52. void Test4029195() 
  53. {
  54.     int32_t resultlength, resultlengthneeded;
  55.     UChar *pat, *fmdt, *todayS, *rt;
  56.     UChar *temp;
  57.     UDate today, d1;
  58.     UDateFormat *df;
  59.     int32_t parsepos;
  60.     UErrorCode status = U_ZERO_ERROR;
  61.  
  62.     log_verbose("Testing date format and parse function in regression test\n");
  63.     today = ucal_getNow();
  64.     
  65.     df = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL, NULL, 0, &status);
  66.     if(U_FAILURE(status))
  67.     {
  68.         log_err("FAIL: error in creating the dateformat using default date and time style : %s\n", myErrorName(status));
  69.     }
  70.     resultlength=0;
  71.     resultlengthneeded=udat_toPattern(df, TRUE, NULL, resultlength, &status);
  72.     if(status==U_BUFFER_OVERFLOW_ERROR)
  73.     {
  74.         status=U_ZERO_ERROR;
  75.         resultlength=resultlengthneeded + 1;
  76.         pat=(UChar*)malloc(sizeof(UChar) * resultlength);
  77.         udat_toPattern(df, TRUE, pat, resultlength, &status);
  78.     }
  79.     
  80.     log_verbose("pattern: %s\n", austrdup(pat));
  81.  
  82.     
  83.     fmdt = myFormatit(df, today);
  84.     log_verbose("today: %s\n", austrdup(fmdt));
  85.     
  86.     temp=(UChar*)malloc(sizeof(UChar) * 10);
  87.     u_uastrcpy(temp, "M yyyy dd");
  88.     udat_applyPattern(df, TRUE, temp, u_strlen(temp));
  89.     
  90.     todayS =myFormatit(df, today);
  91.     log_verbose("After teh pattern is applied\n today: %s\n", austrdup(todayS) );
  92.     parsepos=0;
  93.     d1=udat_parse(df, todayS, u_strlen(todayS), &parsepos, &status);
  94.     if(U_FAILURE(status))
  95.     {
  96.         log_err("FAIL: Error in parsing using udat_parse(.....): %s\n", myErrorName(status));
  97.     }
  98.             
  99.     rt =myFormatit(df, d1);
  100.     log_verbose("today: %s\n", austrdup(rt) );
  101.      
  102.     log_verbose("round trip: %s\n", austrdup(rt) );
  103.       
  104.     if(u_strcmp(rt, todayS)!=0) {
  105.             log_err("Fail: Want  %s  Got  %s\n", austrdup(todayS), austrdup(rt) );
  106.     }
  107.     else
  108.         log_verbose("Pass: parse and format working fine\n");
  109.     udat_close(df);
  110.     free(temp);
  111. }
  112.  
  113.  
  114. /**
  115.  * @bug 4056591
  116.  * Verify the function of the [s|g]et2DigitYearStart() API.
  117.  */
  118. void Test4056591() 
  119. {
  120.     int i;
  121.     UCalendar *cal;
  122.     UDateFormat *def;    
  123.     UDate start,exp,got;
  124.     UChar s[10];
  125.     UChar *gotdate, *expdate;
  126.     UChar pat[10];
  127.     UChar *tzID;
  128.     UDate d[4];
  129.     UErrorCode status = U_ZERO_ERROR;
  130.     const char* strings[] = {
  131.              "091225",
  132.              "091224",
  133.              "611226",
  134.              "991227"
  135.         };
  136.  
  137.     log_verbose("Testing s[get] 2 digit year start regressively\n");
  138.     tzID=(UChar*)malloc(sizeof(UChar) * 4);
  139.     u_uastrcpy(tzID, "PST");
  140.     cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_GREGORIAN, &status);
  141.     if(U_FAILURE(status)){
  142.         log_err("error in ucal_open caldef : %s\n", myErrorName(status));
  143.     }
  144.     ucal_setDateTime(cal, 1809, UCAL_DECEMBER, 25, 17, 40, 30, &status);
  145.     d[0]=ucal_getMillis(cal, &status);
  146.     if(U_FAILURE(status)){
  147.             log_err("Error: failure in get millis: %s\n", myErrorName(status));
  148.     }
  149.     ucal_setDateTime(cal, 1909, UCAL_DECEMBER, 24, 17, 40, 30, &status);
  150.     d[1]=ucal_getMillis(cal, &status);
  151.     ucal_setDateTime(cal, 1861, UCAL_DECEMBER, 26, 17, 40, 30, &status);
  152.     d[2]=ucal_getMillis(cal, &status);
  153.     ucal_setDateTime(cal, 1999, UCAL_DECEMBER, 27, 17, 40, 30, &status);
  154.     d[3]=ucal_getMillis(cal, &status);
  155.  
  156.     
  157.     u_uastrcpy(pat, "yyMMdd");
  158.     def = udat_openPattern(pat, u_strlen(pat), NULL, &status);
  159.     if(U_FAILURE(status))
  160.     {
  161.         log_err("FAIL: error in creating the dateformat using u_openPattern(): %s\n", myErrorName(status));
  162.     }
  163.     start = 1800;
  164.     udat_set2DigitYearStart(def, start, &status);
  165.     if(U_FAILURE(status))
  166.         log_err("ERROR: in setTwoDigitStartDate: %s\n", myErrorName(status));
  167.     if( (udat_get2DigitYearStart(def, &status) != start))
  168.             log_err("ERROR: get2DigitYearStart broken\n");
  169.         
  170.  
  171.         for(i = 0; i < 4; ++i) {
  172.             u_uastrcpy(s, strings[i]);
  173.             exp = d[i];
  174.             got = udat_parse(def, s, u_strlen(s), 0, &status);
  175.             gotdate=myFormatit(def, got);
  176.             expdate=myFormatit(def, exp);
  177.             
  178.             if(u_strcmp(gotdate, expdate) !=0){
  179.                 log_err("set2DigitYearStart broken for %s \n  got: %s, expected: %s\n", austrdup(s),
  180.                     austrdup(gotdate), austrdup(expdate) );
  181.             }
  182.         }
  183.    
  184.  
  185. }
  186.  
  187.  
  188. /**
  189.  * @bug 4059917
  190.  */
  191. void Test4059917() 
  192. {
  193.     UDateFormat* def;
  194.     UChar *myDate;
  195.     UErrorCode status = U_ZERO_ERROR;
  196.     UChar *pattern;
  197.     log_verbose("Testing apply pattern and to pattern regressively\n");
  198.     pattern=(UChar*)malloc(sizeof(UChar) * 11);
  199.     u_uastrcpy(pattern, "yyyy/MM/dd");
  200.     log_verbose("%s\n", austrdup(pattern) );
  201.     def = udat_openPattern(pattern, u_strlen(pattern), NULL, &status);
  202.     if(U_FAILURE(status))
  203.     {
  204.         log_err("FAIL: error in creating the dateformat using openPattern: %s\n", myErrorName(status));
  205.     }
  206.     myDate=(UChar*)malloc(sizeof(UChar) * 11);
  207.     u_uastrcpy(myDate, "1970/01/12");
  208.         
  209.     aux917( def, myDate );
  210.     udat_close(def);
  211.     
  212.     u_uastrcpy(pattern, "yyyyMMdd");
  213.     def = udat_openPattern(pattern, u_strlen(pattern), NULL, &status);
  214.     if(U_FAILURE(status))
  215.     {
  216.         log_err("FAIL: error in creating the dateformat using openPattern: %s\n", myErrorName(status));
  217.     }
  218.     u_uastrcpy(myDate, "19700112");
  219.     aux917( def, myDate );
  220.     udat_close(def);    
  221.     free(pattern);
  222.     free(myDate);
  223.     
  224. }
  225.  
  226. void aux917( UDateFormat *fmt, UChar* str) 
  227. {    
  228.    int32_t resultlength, resultlengthneeded;
  229.    UErrorCode status = U_ZERO_ERROR;
  230.    UChar* formatted;
  231.    UChar *pat;
  232.    UDate d1=1000000000.0;
  233.    
  234.     resultlength=0;
  235.     resultlengthneeded=udat_toPattern(fmt, TRUE, NULL, resultlength, &status);
  236.     if(status==U_BUFFER_OVERFLOW_ERROR)
  237.     {
  238.         status=U_ZERO_ERROR;
  239.         resultlength=resultlengthneeded + 1;
  240.         pat=(UChar*)malloc(sizeof(UChar) * (resultlength));
  241.         udat_toPattern(fmt, TRUE, pat, resultlength, &status);
  242.     }
  243.     if(U_FAILURE(status)){
  244.         log_err("failure in retrieving the pattern: %s\n", myErrorName(status));
  245.     }
  246.     log_verbose("pattern: %s\n", austrdup(pat) );
  247.        
  248.     status = U_ZERO_ERROR;
  249.     formatted = myFormatit(fmt, d1);
  250.     if( u_strcmp(formatted,str)!=0) {
  251.         log_err("Fail: Want %s Got: %s\n", austrdup(str),  austrdup(formatted) );
  252.     }
  253.     
  254. }
  255.  
  256. /**
  257.  * @bug 4060212
  258.  */
  259. void Test4060212() 
  260. {
  261.     int32_t pos;
  262.     UCalendar *cal;
  263.     UDateFormat *formatter, *fmt;
  264.     UErrorCode status = U_ZERO_ERROR;
  265.     UDate myDate;
  266.     UChar *myString;
  267.     UChar dateString[20], pattern[20], tzID[3];
  268.     u_uastrcpy(dateString, "1995-040.05:01:29");
  269.     u_uastrcpy(pattern, "yyyy-DDD.hh:mm:ss");
  270.  
  271.     log_verbose( "dateString= %s Using yyyy-DDD.hh:mm:ss\n", austrdup(dateString) );
  272.     status = U_ZERO_ERROR;
  273.     
  274.     formatter = udat_openPattern(pattern, u_strlen(pattern), NULL, &status);
  275.     pos=0;
  276.     myDate = udat_parse(formatter, dateString, u_strlen(dateString), &pos, &status);
  277.     
  278.     
  279.     fmt = udat_open(UDAT_FULL,UDAT_LONG ,NULL, NULL, 0, &status);
  280.     if(U_FAILURE(status))
  281.     {
  282.         log_err("FAIL: error in creating the dateformat using default date and time style: %s\n", 
  283.                         myErrorName(status) );
  284.     }
  285.     myString = myFormatit(fmt, myDate);
  286.     u_uastrcpy(tzID, "PST");
  287.     cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_GREGORIAN, &status);
  288.     if(U_FAILURE(status)){
  289.         log_err("FAIL: error in ucal_open caldef : %s\n", myErrorName(status));
  290.     }
  291.     ucal_setMillis(cal, myDate, &status);
  292.     if ((ucal_get(cal, UCAL_DAY_OF_YEAR, &status) != 40)){
  293.         log_err("Fail: Got  %d Expected 40\n", ucal_get(cal, UCAL_DAY_OF_YEAR, &status));
  294.     }
  295.     
  296.     udat_close(formatter);
  297.     ucal_close(cal);
  298.     udat_close(fmt);
  299.     
  300. }
  301.  
  302. /**
  303.  * @bug 4061287
  304.  */
  305. void Test4061287() 
  306. {
  307.     bool_t ok;
  308.     int32_t pos;
  309.     UDateFormat *df;
  310.     UErrorCode status = U_ZERO_ERROR;
  311.     UDate myDate;
  312.     UChar pattern[21], dateString[11];
  313.     
  314.     u_uastrcpy(dateString, "35/13/1971");
  315.     u_uastrcpy(pattern, "dd/mm/yyyy");
  316.     status = U_ZERO_ERROR;
  317.     log_verbose("Testing parsing by changing the attribute lenient\n");
  318.     df = udat_openPattern(pattern, u_strlen(pattern), NULL, &status);
  319.     if(U_FAILURE(status)){
  320.         log_err("ERROR: failure in open pattern of test4061287: %s\n", myErrorName(status));
  321.     }
  322.  
  323.     pos=0;
  324.     
  325.     udat_setLenient(df, FALSE);
  326.        ok=udat_isLenient(df);
  327.     if(ok==TRUE)
  328.         log_err("setLenient nor working\n");
  329.     ok = FALSE;
  330.     myDate = udat_parse(df, dateString, u_strlen(dateString), &pos, &status);
  331.     if(U_FAILURE(status))
  332.         ok = TRUE;
  333.     if(ok!=TRUE) 
  334.         log_err("Fail: Lenient not working: does lenient parsing in spite of setting Leninent as FALSE ");
  335.     
  336. }
  337.  
  338.  
  339.  
  340. /* The java.text.DateFormat.parse(String) method expects for the
  341.   US locale a string formatted according to mm/dd/yy and parses it
  342.   correctly.
  343.  
  344.   When given a string mm/dd/yyyy it only parses up to the first
  345.   two y's, typically resulting in a date in the year 1919.
  346.   
  347.   Please extend the parsing method(s) to handle strings with
  348.   four-digit year values (probably also applicable to various
  349.   other locales.  */
  350. /**
  351.  * @bug 4073003
  352.  */
  353. void Test4073003() 
  354. {
  355.     int32_t pos,i;
  356.     UDate d,dd;
  357.     UChar *datestr;
  358.     UChar temp[15];
  359.     UErrorCode status = U_ZERO_ERROR;
  360.     UDateFormat *fmt;
  361.     UChar *result, *result2;
  362.     const char* tests [] = { 
  363.                 "12/25/61", 
  364.                 "12/25/1961", 
  365.                 "4/3/1999", 
  366.                 "4/3/99" 
  367.         };
  368.     
  369.     fmt= udat_open(UDAT_SHORT,UDAT_SHORT ,NULL, NULL, 0, &status);
  370.     if(U_FAILURE(status))
  371.     {
  372.         log_err("FAIL: error in creating the dateformat using short date and time style: %s\n", 
  373.             myErrorName(status));
  374.     }
  375.     u_uastrcpy(temp, "m/D/yy");
  376.     udat_applyPattern(fmt, TRUE, temp, u_strlen(temp));
  377.     
  378.         for(i= 0; i < 4; i+=2) {
  379.             status=U_ZERO_ERROR;
  380.             datestr=(UChar*)malloc(sizeof(UChar) * (strlen(tests[i])+1));
  381.             u_uastrcpy(datestr, tests[i]);
  382.             
  383.             pos=0;
  384.             d = udat_parse(fmt, datestr, u_strlen(datestr), &pos, &status);
  385.             if(U_FAILURE(status)){
  386.                 log_err("ERROR : in test 4073003: %s\n", myErrorName(status));
  387.             }
  388.             
  389.             free(datestr);
  390.             datestr=(UChar*)malloc(sizeof(UChar) * (strlen(tests[i+1])+1));
  391.             u_uastrcpy(datestr, tests[i+1]);
  392.         
  393.             pos=0;
  394.             status=U_ZERO_ERROR;
  395.             dd = udat_parse(fmt, datestr, u_strlen(datestr), &pos, &status);
  396.             if(U_FAILURE(status)){
  397.                 log_err("ERROR : in test 4073003: %s\n", myErrorName(status));
  398.             }
  399.             free(datestr);
  400.             
  401.             result =myFormatit(fmt, d);
  402.             result2 =myFormatit(fmt, dd);
  403.             if (u_strcmp(result, result2)!=0){
  404.                 log_err("Fail: %s != %s\n", austrdup(result), austrdup(result2) );
  405.                  }
  406.             else{
  407.             log_verbose("Ok: %s == %s\n", austrdup(result), austrdup(result2) );
  408.             }
  409.             
  410.             
  411.        
  412.         }
  413. }
  414.  
  415. /**
  416.  * @bug 4162071
  417.  **/
  418. void Test4162071() 
  419. {
  420.     int32_t pos;
  421.     UDate x;
  422.     UErrorCode status = U_ZERO_ERROR;
  423.     UDateFormat *df;
  424.     UChar datestr[30];
  425.     UChar format[50];
  426.     u_uastrcpy(datestr, "Thu, 30-Jul-1999 11:51:14 GMT");
  427.     u_uastrcpy(format, "EEE', 'dd-MMM-yyyy HH:mm:ss z"); /*  RFC 822/1123 */
  428.     status = U_ZERO_ERROR;
  429.     /* Can't hardcode the result to assume the default locale is "en_US". */
  430.     df = udat_openPattern(format, u_strlen(format), "en_US", &status);
  431.     if(U_FAILURE(status)){
  432.         log_err("ERROR: couldn't create date format: %s\n", myErrorName(status));
  433.     }
  434.   pos=0;
  435.     x = udat_parse(df, datestr, u_strlen(datestr), &pos, &status);
  436.     if(U_FAILURE(status)){
  437.                 log_err("ERROR : parse format  %s failes : %s\n", austrdup(format), myErrorName(status));
  438.             }
  439.     else{
  440.         log_verbose("Parse format \"%s \" ok.\n", austrdup(format) );
  441.     }
  442.     log_verbose("date= %s\n", austrdup(myFormatit(df, x)) );    
  443. }
  444.  
  445. /*INTERNAL FUNCTION USED */
  446.  
  447. UChar* myFormatit(UDateFormat* datdef, UDate d1)
  448. {
  449.     UChar *result1;
  450.     int32_t resultlength, resultlengthneeded;
  451.     UFieldPosition pos;
  452.     UErrorCode status = U_ZERO_ERROR;
  453.     
  454.     resultlength=0;
  455.     resultlengthneeded=udat_format(datdef, d1, NULL, resultlength, &pos, &status);
  456.     if(status==U_BUFFER_OVERFLOW_ERROR)
  457.     {
  458.         status=U_ZERO_ERROR;
  459.         resultlength=resultlengthneeded+1;
  460.         result1=(UChar*)malloc(sizeof(UChar) * resultlength);
  461.         udat_format(datdef, d1, result1, resultlength, &pos, &status);
  462.     }
  463.     if(U_FAILURE(status))
  464.     {
  465.         log_err("FAIL: Error in formatting using udat_format(.....): %s\n", myErrorName(status));
  466.         return 0;
  467.     }
  468.     
  469.     
  470.     return result1;
  471.  
  472. }
  473.  
  474. /*eof*/
  475.