home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / cintltst / cdtdptst.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  9.9 KB  |  280 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 CDTDPTST.C
  15. *
  16. * Modification History:
  17. *        Name                     Description            
  18. *     Madhu Katragadda               Creation
  19. *********************************************************************************
  20. */
  21. /* INDEPTH TEST FOR DATE FORMAT */
  22.  
  23. #include "uloc.h"
  24. #include "utypes.h"
  25. #include "udat.h"
  26. #include "ucal.h"
  27. #include "unum.h"
  28. #include "ustring.h"
  29. #include "cintltst.h"
  30. #include "cdtdptst.h"
  31. #include "cformtst.h"
  32. #include <stdio.h>
  33. #include <string.h>
  34.  
  35. void addDtFrDepTest(TestNode** root)
  36. {
  37.     addTest(root, &TestTwoDigitYearDSTParse, "tsformat/cdtdptst/TestTwoDigitYearDSTParse");
  38.     addTest(root, &TestPartialParse994, "tsformat/cdtdptst/TestPartialParse994");
  39.     addTest(root, &TestRunTogetherPattern985, "tsformat/cdtdptst/TestRunTogetherPattern985");
  40.     addTest(root, &TestCzechMonths459, "tsformat/cdtdptst/TestCzechMonths459");
  41.     addTest(root, &TestQuotePattern161, "tsformat/cdtdptst/TestQuotePattern161");
  42.     
  43. }
  44.  
  45. /**
  46.  * Test the parsing of 2-digit years.
  47.  */
  48. void TestTwoDigitYearDSTParse()
  49. {
  50.     UDateFormat *fullFmt, *fmt;
  51.     UErrorCode status = U_ZERO_ERROR;
  52.     UChar *pattern;
  53.     UDate d;
  54.     UChar *str;
  55.     UChar *s;
  56.     int32_t pos;
  57.     UChar *res;
  58.  
  59.     pattern=(UChar*)malloc(sizeof(UChar) * (strlen("EEE MMM dd HH:mm:ss.SSS zzz yyyy G")+1 ));
  60.     u_uastrcpy(pattern, "EEE MMM dd HH:mm:ss.SSS zzz yyyy G");
  61.     fullFmt= udat_openPattern(pattern, u_strlen(pattern), "en_US", &status);
  62.     if(U_FAILURE(status))    {
  63.         log_err("FAIL: Error in creating a date format using udat_openPattern \n %s\n", 
  64.             myErrorName(status) );
  65.     }
  66.     else {log_verbose("PASS: creating dateformat using udat_openPattern() succesful\n");}
  67.     
  68.     u_uastrcpy(pattern, "dd-MMM-yy h:mm:ss 'o''clock' a z");
  69.     fmt= udat_openPattern(pattern, u_strlen(pattern), "en_US", &status);
  70.     
  71.     
  72.     s=(UChar*)malloc(sizeof(UChar) * (strlen("03-Apr-04 2:20:47 o'clock AM PST")+1) );
  73.     u_uastrcpy(s, "03-Apr-04 2:20:47 o'clock AM PST");
  74.     pos=0;
  75.     d = udat_parse(fmt, s, u_strlen(s), &pos, &status);
  76.     
  77.     res=(UChar*)malloc(sizeof(UChar) * (strlen("Sat Apr 03 02:20:47.000 PST 2004 AD")+1) );
  78.     u_uastrcpy(res, "Sat Apr 03 02:20:47.000 PST 2004 AD");
  79.     str=myDateFormat(fullFmt, d);
  80.     if(u_strcmp(str, res) != 0)
  81.         log_err("Error in parsing and format two digit year with different patterns\n");
  82.     
  83.     
  84.     udat_close(fullFmt);
  85.     udat_close(fmt);
  86.     free(pattern);
  87.     free(s);
  88. }
  89.  
  90.  
  91. /**
  92.  * Verify that strings which contain incomplete specifications are parsed
  93.  * correctly.  In some instances, this means not being parsed at all, and
  94.  * returning an appropriate error.
  95.  */
  96. void TestPartialParse994()
  97. {
  98.     int32_t pos;
  99.     UDateFormat *f;
  100.     UErrorCode status = U_ZERO_ERROR;
  101.     UChar *s;
  102.     UDate d, null;
  103.     null=0;
  104.  
  105.     f = udat_open(UDAT_DEFAULT, UDAT_SHORT, NULL, NULL, 0, &status);
  106.     if(U_FAILURE(status)){
  107.         log_err("FAIL: ErrorCode received during test: %s\n", myErrorName(status));
  108.     }
  109.     s=(UChar*)malloc(sizeof(UChar) * (strlen("01/01/97 10:11:42 AM")+1) );
  110.     u_uastrcpy(s, "01/01/97 10:11:42 AM");
  111.     pos=0;
  112.     d = udat_parse(f, s, u_strlen(s), &pos, &status);
  113.     log_verbose("%s\n", austrdup(myDateFormat(f, d)) );
  114.     tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/01 10:11:42", d);
  115.     tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/01 10:", null);
  116.     tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/01 10", null);
  117.     tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/01 ", null);
  118.     tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/01", null);
  119.     udat_close(f);
  120. }
  121.  
  122.  
  123.  
  124. void tryPat994(UDateFormat* format, const char* pattern, const char* s, UDate expected)
  125. {
  126.     UChar *f;
  127.     UChar *str, *pat;
  128.     UDate date;
  129.     UDate null=0;
  130.     int32_t pos;
  131.     UErrorCode status = U_ZERO_ERROR;
  132.     str=(UChar*)malloc(sizeof(UChar) * (strlen(s) + 1) );
  133.     u_uastrcpy(str, s);
  134.     pat=(UChar*)malloc(sizeof(UChar) * (strlen(pattern) + 1) );
  135.     u_uastrcpy(pat, pattern);
  136.     log_verbose("Pattern : %s ;  String : %s\n", austrdup(pat), austrdup(str));
  137.     udat_applyPattern(format, FALSE, pat, u_strlen(pat));
  138.     pos=0;
  139.     date = udat_parse(format, str, u_strlen(str), &pos, &status);
  140.     if(U_FAILURE(status) || date == null) {
  141.         log_verbose("ParseException: : %s\n", myErrorName(status) );
  142.          if (expected != null) 
  143.              log_err("FAIL: Expected: %s\n", austrdup(myDateFormat(format, expected)) );
  144.         }
  145.     else {
  146.             
  147.         f=myDateFormat(format, date);
  148.         log_verbose(" parse( %s ) -> %s\n", austrdup(str), austrdup(f));
  149.         if (expected == null || !(date == expected)) 
  150.             log_err("FAIL: Expected null");
  151.         if (u_strcmp(f, str) !=0) log_err("FAIL: Expected : %s\n", austrdup(str) );
  152.         }
  153.     
  154.     free(str);
  155.     free(pat);
  156. }
  157.  
  158.  
  159. /**
  160.  * Verify the behavior of patterns in which digits for different fields run together
  161.  * without intervening separators.
  162.  */
  163. void TestRunTogetherPattern985()
  164. {
  165.     int32_t pos;
  166.     UChar *pattern, *now, *then;
  167.     UDateFormat *format;
  168.     UDate date1, date2;
  169.     UErrorCode status = U_ZERO_ERROR;
  170.     pattern=(UChar*)malloc(sizeof(UChar) * (strlen("yyyyMMddHHmmssSSS")+1) );
  171.     u_uastrcpy(pattern, "yyyyMMddHHmmssSSS");
  172.     format = udat_openPattern(pattern, u_strlen(pattern), NULL, &status);
  173.     if(U_FAILURE(status)){
  174.         log_err("FAIL: Error in date format construction with pattern: %s\n", myErrorName(status));
  175.     }
  176.     date1 = ucal_getNow();
  177.     now=myDateFormat(format, date1);
  178.     log_verbose("%s\n", austrdup(now) ); 
  179.     pos = 0;
  180.     date2 = udat_parse(format, now, u_strlen(now), &pos, &status);
  181.     if (date2 == 0) log_verbose("Parse stopped at : %d\n", pos);
  182.     else then=myDateFormat(format, date2);
  183.     log_verbose("%s\n", austrdup(then) );
  184.     if (!(date2 == date1)) log_err("FAIL\n");
  185.     
  186.     udat_close(format);
  187.     free(pattern);
  188.        
  189. }
  190.  
  191. /**
  192.  * Verify the handling of Czech June and July, which have the unique attribute that
  193.  * one is a proper prefix substring of the other.
  194.  */
  195. void TestCzechMonths459()
  196. {
  197.     int32_t lneed, pos;
  198.     UChar *pattern, *tzID;
  199.     UChar *juneStr, *julyStr;
  200.     UDateFormat *fmt;
  201.     UCalendar *cal;
  202.     UDate june, july, d;
  203.     UErrorCode status = U_ZERO_ERROR;
  204.     
  205.     fmt = udat_open(UDAT_FULL, UDAT_FULL, "cs", NULL, 0, &status);
  206.     if(U_FAILURE(status)){log_err("Error in constructing the date format\n"); }
  207.     lneed=0;
  208.     lneed=udat_toPattern(fmt, TRUE, NULL, lneed, &status);
  209.     if(status==U_BUFFER_OVERFLOW_ERROR){
  210.         status=U_ZERO_ERROR;
  211.         pattern=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
  212.         udat_toPattern(fmt, TRUE, pattern, lneed+1, &status);
  213.     }
  214.     if(U_FAILURE(status)){ log_err("Error in extracting the pattern\n"); }
  215.     tzID=(UChar*)malloc(sizeof(UChar) * 4);
  216.     u_uastrcpy(tzID, "GMT");
  217.     cal=ucal_open(tzID, u_strlen(tzID), "cs", UCAL_GREGORIAN, &status);
  218.     if(U_FAILURE(status)){ log_err("error in ucal_open caldef : %s\n", myErrorName(status));    }
  219.     
  220.     ucal_setDate(cal, 1997, UCAL_JUNE, 15, &status);
  221.     june=ucal_getMillis(cal, &status);
  222.     ucal_setDate(cal, 1997, UCAL_JULY, 15, &status);
  223.     july=ucal_getMillis(cal, &status);
  224.  
  225.     
  226.     juneStr = myDateFormat(fmt, june);
  227.     julyStr = myDateFormat(fmt, july);
  228.     pos=0;
  229.     d = udat_parse(fmt, juneStr, u_strlen(juneStr), &pos, &status);
  230.     if(u_strcmp(myDateFormat(fmt, june), myDateFormat(fmt, d) ) !=0)
  231.         log_err("Error in handling the czech month june\n");
  232.     else
  233.         log_verbose("Pass: Date = %s\n",  austrdup(myDateFormat(fmt, d)) );
  234.     pos=0;
  235.     d = udat_parse(fmt, julyStr, u_strlen(julyStr), &pos, &status);
  236.     if(u_strcmp(myDateFormat(fmt, july), myDateFormat(fmt, d) ) !=0)
  237.         log_err("Error in handling the czech month july\n");
  238.     else
  239.         log_verbose("Pass: Date = %s\n",  austrdup(myDateFormat(fmt, d)) );
  240.     
  241.     udat_close(fmt);
  242.     ucal_close(cal);
  243.     free(pattern);
  244.     free(tzID);
  245. }
  246.  
  247. /**
  248.  * Test the handling of single quotes in patterns.
  249.  */
  250. void TestQuotePattern161()
  251. {
  252.     UDateFormat *format;
  253.     UCalendar *cal;
  254.     UDate currentTime_1;
  255.     UChar *pattern, *tzID, *exp;
  256.     UChar *dateString;
  257.     UErrorCode status = U_ZERO_ERROR;
  258.     pattern=(UChar*)malloc(sizeof(UChar) * (strlen("MM/dd/yyyy 'at' hh:mm:ss a zzz")+1) );
  259.     u_uastrcpy(pattern, "MM/dd/yyyy 'at' hh:mm:ss a zzz");
  260.     
  261.     format= udat_openPattern(pattern, u_strlen(pattern), NULL, &status);
  262.     if(U_FAILURE(status)){ log_err("error in udat_openPattern  : %s\n", myErrorName(status));    }
  263.     tzID=(UChar*)malloc(sizeof(UChar) * 4);
  264.     u_uastrcpy(tzID, "PST");
  265.     cal=ucal_open(tzID, u_strlen(tzID), NULL, UCAL_TRADITIONAL, &status);
  266.     if(U_FAILURE(status)){ log_err("error in ucal_open cal : %s\n", myErrorName(status));    }
  267.     
  268.     ucal_setDateTime(cal, 1999, UCAL_APRIL, 13, 10, 42, 28, &status);
  269.     currentTime_1 = ucal_getMillis(cal, &status);
  270.     
  271.     dateString = myDateFormat(format, currentTime_1);
  272.     exp=(UChar*)malloc(sizeof(UChar) * (strlen("04/13/1999 at 10:42:28 AM PDT") + 1) );
  273.     u_uastrcpy(exp, "04/13/1999 at 10:42:28 AM PDT");
  274.     
  275.     log_verbose("%s\n", austrdup(dateString) );
  276.     if(u_strcmp(dateString, exp) !=0)
  277.         log_err("Error in formatting a pattern with single quotes\n");
  278.     
  279. }
  280.