~ubuntu-branches/ubuntu/raring/clucene-core/raring-proposed

« back to all changes in this revision

Viewing changes to src/test/document/TestDateTools.cpp

  • Committer: Package Import Robot
  • Author(s): Fathi Boudra
  • Date: 2012-08-11 09:33:38 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20120811093338-fgrx41ftqew3qt6a
Tags: 2.3.3.4-1
* New upstream release (Closes: #661703).
* Convert package to multiarch.
* Drop obsolete patches:
  - 01_add_missing_include_bug505667.diff
  - 02_posixness_fix_bug530308.diff
* Add patches:
  - Fixing_ZLIB_configuration_in_shared_CMakeLists.patch
  - Fix-pkgconfig-file-by-adding-clucene-shared-library.patch
  - Install-contribs-lib.patch
  - multiarch.patch
* Update debian/compat: bump to 8.
* Update debian/control:
  - update build dependencies (add cmake, libboost-dev and libz-dev).
  - bump Standards-Version to 3.9.3.
  - rename packages due to ABI bump: libclucene0ldbl -> libclucene-core1.
  - add libclucene-contribs1 package.
* Update debian/rules:
  - rewrite to use CMake.
  - add multiarch support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*------------------------------------------------------------------------------
 
2
* Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
 
3
 
4
* Distributable under the terms of either the Apache License (Version 2.0) or 
 
5
* the GNU Lesser General Public License, as specified in the COPYING file.
 
6
------------------------------------------------------------------------------*/
 
7
#include "test.h"
 
8
 
 
9
void assertDateTimeEquals(CuTest* tc, const TCHAR* isoFormat, int64_t d){
 
10
    TCHAR* tmp = DateTools::getISOFormat(d);
 
11
    int res = _tcscmp(isoFormat, tmp);
 
12
    _CLDELETE_LCARRAY(tmp);
 
13
    CLUCENE_ASSERT(res == 0);
 
14
}
 
15
 
 
16
void testStringToDate(CuTest *tc) {
 
17
    int64_t d = DateTools::stringToTime(_T("2004"));
 
18
    assertDateTimeEquals(tc, _T("2004-01-01 00:00:00:000"), d);
 
19
    d = DateTools::stringToTime(_T("20040705"));
 
20
    assertDateTimeEquals(tc, _T("2004-07-05 00:00:00:000"), d);
 
21
    d = DateTools::stringToTime(_T("200407050910"));
 
22
    assertDateTimeEquals(tc, _T("2004-07-05 09:10:00:000"), d);
 
23
    d = DateTools::stringToTime(_T("20040705091055990"));
 
24
    assertDateTimeEquals(tc, _T("2004-07-05 09:10:55:990"), d);
 
25
 
 
26
    try {
 
27
        d = DateTools::stringToTime(_T("97"));    // no date
 
28
        CuFail(tc, _T("Expected an exception on DateTools::stringToTime(\"97\")"));
 
29
    } catch(...) { /* expected exception */ }
 
30
    
 
31
    try {
 
32
        d = DateTools::stringToTime(_T("200401011235009999"));    // no date
 
33
        CuFail(tc, _T("Expected an exception on DateTools::stringToTime(\"200401011235009999\")"));
 
34
    } catch(...) { /* expected exception */ }
 
35
    
 
36
    try {
 
37
        d = DateTools::stringToTime(_T("aaaa"));    // no date
 
38
        CuFail(tc, _T("Expected an exception on DateTools::stringToTime(\"aaaa\")"));
 
39
    } catch(...) { /* expected exception */ }
 
40
}
 
41
 
 
42
void testDateAndTimetoString(CuTest* tc) {
 
43
    int64_t cal = DateTools::getTime(2004, 2, 3, 22, 8, 56, 333);
 
44
    TCHAR* dateString;
 
45
 
 
46
    dateString = DateTools::timeToString(cal, DateTools::YEAR_FORMAT);
 
47
    CLUCENE_ASSERT( _tcscmp(_T("2004"), dateString) == 0 );
 
48
    assertDateTimeEquals(tc, _T("2004-01-01 00:00:00:000"), DateTools::stringToTime(dateString));
 
49
    _CLDELETE_LCARRAY(dateString);
 
50
 
 
51
    dateString = DateTools::timeToString(cal, DateTools::MONTH_FORMAT);
 
52
    CLUCENE_ASSERT( _tcscmp(_T("200402"), dateString) == 0 );
 
53
    assertDateTimeEquals(tc, _T("2004-02-01 00:00:00:000"), DateTools::stringToTime(dateString));
 
54
    _CLDELETE_LCARRAY(dateString);
 
55
 
 
56
    dateString = DateTools::timeToString(cal, DateTools::DAY_FORMAT);
 
57
    CLUCENE_ASSERT( _tcscmp(_T("20040203"), dateString) == 0 );
 
58
    assertDateTimeEquals(tc, _T("2004-02-03 00:00:00:000"), DateTools::stringToTime(dateString));
 
59
    _CLDELETE_LCARRAY(dateString);
 
60
 
 
61
    dateString = DateTools::timeToString(cal, DateTools::HOUR_FORMAT);
 
62
    CLUCENE_ASSERT( _tcscmp(_T("2004020322"), dateString) == 0 );
 
63
    assertDateTimeEquals(tc, _T("2004-02-03 22:00:00:000"), DateTools::stringToTime(dateString));
 
64
    _CLDELETE_LCARRAY(dateString);
 
65
 
 
66
    dateString = DateTools::timeToString(cal, DateTools::MINUTE_FORMAT);
 
67
    CLUCENE_ASSERT( _tcscmp(_T("200402032208"), dateString) == 0 );
 
68
    assertDateTimeEquals(tc, _T("2004-02-03 22:08:00:000"), DateTools::stringToTime(dateString));
 
69
    _CLDELETE_LCARRAY(dateString);
 
70
 
 
71
    dateString = DateTools::timeToString(cal, DateTools::SECOND_FORMAT);
 
72
    CLUCENE_ASSERT( _tcscmp(_T("20040203220856"), dateString) == 0 );
 
73
    assertDateTimeEquals(tc, _T("2004-02-03 22:08:56:000"), DateTools::stringToTime(dateString));
 
74
    _CLDELETE_LCARRAY(dateString);
 
75
 
 
76
    dateString = DateTools::timeToString(cal, DateTools::MILLISECOND_FORMAT);
 
77
    CLUCENE_ASSERT( _tcscmp(_T("20040203220856333"), dateString) == 0 );
 
78
    assertDateTimeEquals(tc, _T("2004-02-03 22:08:56:333"), DateTools::stringToTime(dateString));
 
79
    _CLDELETE_LCARRAY(dateString);
 
80
 
 
81
    /*
 
82
    // TODO: Allow this
 
83
 
 
84
    // date before 1970:
 
85
    cal.set(1961, 2, 5,   // year=1961, month=march(!), day=5
 
86
        23, 9, 51);       // hour, minute, second
 
87
    cal.set(Calendar.MILLISECOND, 444);
 
88
    dateString = DateTools.dateToString(cal.getTime(), DateTools.Resolution.MILLISECOND);
 
89
    assertDateTimeEquals("19610305230951444", dateString);
 
90
    assertDateTimeEquals("1961-03-05 23:09:51:444", isoFormat(DateTools.stringToDate(dateString)));
 
91
 
 
92
    dateString = DateTools.dateToString(cal.getTime(), DateTools.Resolution.HOUR);
 
93
    assertDateTimeEquals("1961030523", dateString);
 
94
    assertDateTimeEquals("1961-03-05 23:00:00:000", isoFormat(DateTools.stringToDate(dateString)));
 
95
 
 
96
    // timeToString:
 
97
    cal.set(1970, 0, 1, // year=1970, month=january, day=1
 
98
        0, 0, 0); // hour, minute, second
 
99
    cal.set(Calendar.MILLISECOND, 0);
 
100
    dateString = DateTools.timeToString(cal.getTime().getTime(),
 
101
        DateTools.Resolution.MILLISECOND);
 
102
    assertDateTimeEquals("19700101000000000", dateString);
 
103
 
 
104
    cal.set(1970, 0, 1, // year=1970, month=january, day=1
 
105
        1, 2, 3); // hour, minute, second
 
106
    cal.set(Calendar.MILLISECOND, 0);
 
107
    dateString = DateTools.timeToString(cal.getTime().getTime(),
 
108
        DateTools.Resolution.MILLISECOND);
 
109
    assertDateTimeEquals("19700101010203000", dateString);
 
110
    */
 
111
 
112
 
 
113
CuSuite *testDateTools(void)
 
114
{
 
115
    CuSuite *suite = CuSuiteNew(_T("CLucene DateTools Test"));
 
116
 
 
117
    SUITE_ADD_TEST(suite, testStringToDate);
 
118
    // SUITE_ADD_TEST(suite, testStringtoTime); -- tests against a calendar object - irrelevant to clucene
 
119
    SUITE_ADD_TEST(suite, testDateAndTimetoString);
 
120
 
 
121
    return suite;
 
122
}