File: utils_dates_cunit.c

package info (click to toggle)
grisbi 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 15,180 kB
  • ctags: 7,111
  • sloc: ansic: 114,821; sh: 11,374; makefile: 1,102; perl: 370; yacc: 291
file content (225 lines) | stat: -rw-r--r-- 8,824 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* ************************************************************************** */
/*                                                                            */
/*                                  utils_dates_cunit                         */
/*                                                                            */
/*     Copyright (C)    2000-2007 Cédric Auger (cedric@grisbi.org)            */
/*          2003-2008 Benjamin Drieu (bdrieu@april.org)	                      */
/*                      2009 Mickaël Remars (grisbi@remars.com)               */
/*          http://www.grisbi.org                                             */
/*                                                                            */
/*  This program is free software; you can redistribute it and/or modify      */
/*  it under the terms of the GNU General Public License as published by      */
/*  the Free Software Foundation; either version 2 of the License, or         */
/*  (at your option) any later version.                                       */
/*                                                                            */
/*  This program is distributed in the hope that it will be useful,           */
/*  but WITHOUT ANY WARRANTY; without even the implied warranty of            */
/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             */
/*  GNU General Public License for more details.                              */
/*                                                                            */
/*  You should have received a copy of the GNU General Public License         */
/*  along with this program; if not, write to the Free Software               */
/*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
/*                                                                            */
/* ************************************************************************** */

/**
 * \file utils_dates_cunit.c
 * cunit tests for utils_dates
 */

#include "include.h"

/* START_INCLUDE */
#include "utils_dates_cunit.h"
#include "./utils_dates.h"
/* END_INCLUDE */

/* START_STATIC */
static void utils_dates_cunit__gsb_parse_date_string ( void );
static int utils_dates_cunit_clean_suite ( void );
static int utils_dates_cunit_init_suite ( void );
/* END_STATIC */

/* START_EXTERN */
/* END_EXTERN */

/* The suite initialization function.
 * Returns zero on success, non-zero otherwise.
 */
int utils_dates_cunit_init_suite ( void )
{
    return 0;
}

/* The suite cleanup function.
 * Returns zero on success, non-zero otherwise.
 */
int utils_dates_cunit_clean_suite ( void )
{
    return 0;
}

void utils_dates_cunit__gsb_parse_date_string ( void )
{
    GDate *date = NULL;
    char *lc_time_orig;
    char *result = setlocale(LC_TIME, NULL);
    if (result != NULL)
    {
        lc_time_orig = (char *)malloc((strlen(result) + 1) * sizeof(char));
        strcpy(lc_time_orig, result);

        /* C test */
        result = setlocale(LC_TIME, "C");
        if (result != NULL)
        {
            // invalid day
            CU_ASSERT_EQUAL ( NULL, gsb_parse_date_string ( "02/00/2009" ) );

            date = gsb_parse_date_string ( "01/02/2009" );
            CU_ASSERT_EQUAL(2009, g_date_get_year(date));
            CU_ASSERT_EQUAL(1, g_date_get_month(date));
            CU_ASSERT_EQUAL(2, g_date_get_day(date));

            // invalid day
            CU_ASSERT_EQUAL ( NULL, gsb_parse_date_string ( "02/30/2009" ) );

            // 2009 is not a leap year
            CU_ASSERT_EQUAL ( NULL, gsb_parse_date_string ( "02/29/2009" ) );

            // 2008 was a leap year
            date = gsb_parse_date_string ( "02/29/2008" );
            CU_ASSERT_EQUAL(2008, g_date_get_year(date));
            CU_ASSERT_EQUAL(2, g_date_get_month(date));
            CU_ASSERT_EQUAL(29, g_date_get_day(date));

            date = gsb_parse_date_string ( "02/28/2009" );
            CU_ASSERT_EQUAL(2009, g_date_get_year(date));
            CU_ASSERT_EQUAL(2, g_date_get_month(date));
            CU_ASSERT_EQUAL(28, g_date_get_day(date));

            date = gsb_parse_date_string ( "12/31/2009" );
            CU_ASSERT_EQUAL(2009, g_date_get_year(date));
            CU_ASSERT_EQUAL(12, g_date_get_month(date));
            CU_ASSERT_EQUAL(31, g_date_get_day(date));

            // invalid day
            CU_ASSERT_EQUAL ( NULL, gsb_parse_date_string ( "12/32/2009" ) );

            // invalid month
            CU_ASSERT_EQUAL ( NULL, gsb_parse_date_string ( "13/13/2009" ) );
        }

        /* French test or english GB test*/
        result = setlocale(LC_TIME, "fr_FR.UTF-8");
        if (result == NULL)
            result = setlocale(LC_TIME, "en_GB.UTF-8");
        if (result == NULL)
            result = setlocale(LC_TIME, "fr_FR@euro");
        if (result == NULL)
            result = setlocale(LC_TIME, "fr_FR");
        if (result == NULL)
            result = setlocale(LC_TIME, "en_GB");
        if (result != NULL)
        {
            // invalid day
            CU_ASSERT_EQUAL ( NULL, gsb_parse_date_string ( "00/02/2009" ) );

            date = gsb_parse_date_string ( "01/02/2009" );
            CU_ASSERT_EQUAL(2009, g_date_get_year(date));
            CU_ASSERT_EQUAL(2, g_date_get_month(date));
            CU_ASSERT_EQUAL(1, g_date_get_day(date));

            // invalid day
            CU_ASSERT_EQUAL ( NULL, gsb_parse_date_string ( "30/02/2009" ) );

            // 2009 is not a leap year
            CU_ASSERT_EQUAL ( NULL, gsb_parse_date_string ( "29/02/2009" ) );

            // 2008 was a leap year
            date = gsb_parse_date_string ( "29/02/2008" );
            CU_ASSERT_EQUAL(2008, g_date_get_year(date));
            CU_ASSERT_EQUAL(2, g_date_get_month(date));
            CU_ASSERT_EQUAL(29, g_date_get_day(date));

            date = gsb_parse_date_string ( "28/02/2009" );
            CU_ASSERT_EQUAL(2009, g_date_get_year(date));
            CU_ASSERT_EQUAL(2, g_date_get_month(date));
            CU_ASSERT_EQUAL(28, g_date_get_day(date));

            date = gsb_parse_date_string ( "31/12/2009" );
            CU_ASSERT_EQUAL(2009, g_date_get_year(date));
            CU_ASSERT_EQUAL(12, g_date_get_month(date));
            CU_ASSERT_EQUAL(31, g_date_get_day(date));

            // invalid day
            CU_ASSERT_EQUAL ( NULL, gsb_parse_date_string ( "32/12/2009" ) );

            // invalid month
            CU_ASSERT_EQUAL ( NULL, gsb_parse_date_string ( "13/13/2009" ) );
        }

        /* English US test */
        result = setlocale(LC_TIME, "en_US.UTF-8");
        if (result == NULL)
            result = setlocale(LC_TIME, "en_US");
        if (result != NULL)
        {
            // invalid day
            CU_ASSERT_EQUAL ( NULL, gsb_parse_date_string ( "02/00/2009" ) );

            date = gsb_parse_date_string ( "02/01/2009" );
            CU_ASSERT_EQUAL(2009, g_date_get_year(date));
            CU_ASSERT_EQUAL(2, g_date_get_month(date));
            CU_ASSERT_EQUAL(1, g_date_get_day(date));

            // invalid day
            CU_ASSERT_EQUAL ( NULL, gsb_parse_date_string ( "02/30/2009" ) );

            // 2009 is not a leap year
            CU_ASSERT_EQUAL ( NULL, gsb_parse_date_string ( "02/29/2009" ) );

            // 2008 was a leap year
            date = gsb_parse_date_string ( "02/29/2008" );
            CU_ASSERT_EQUAL(2008, g_date_get_year(date));
            CU_ASSERT_EQUAL(2, g_date_get_month(date));
            CU_ASSERT_EQUAL(29, g_date_get_day(date));

            date = gsb_parse_date_string ( "02/28/2009" );
            CU_ASSERT_EQUAL(2009, g_date_get_year(date));
            CU_ASSERT_EQUAL(2, g_date_get_month(date));
            CU_ASSERT_EQUAL(28, g_date_get_day(date));

            date = gsb_parse_date_string ( "12/31/2009" );
            CU_ASSERT_EQUAL(2009, g_date_get_year(date));
            CU_ASSERT_EQUAL(12, g_date_get_month(date));
            CU_ASSERT_EQUAL(31, g_date_get_day(date));

            // invalid day
            CU_ASSERT_EQUAL ( NULL, gsb_parse_date_string ( "12/32/2009" ) );

            // invalid month
            CU_ASSERT_EQUAL ( NULL, gsb_parse_date_string ( "13/13/2009" ) );
        }

        /* Restore current locale and free memory */
        setlocale(LC_TIME, lc_time_orig);
        free(lc_time_orig) ;
    }
}

CU_pSuite utils_dates_cunit_create_suite ( void )
{
    CU_pSuite pSuite = CU_add_suite("utils_dates",
                                    utils_dates_cunit_init_suite,
                                    utils_dates_cunit_clean_suite);
    if ( NULL == pSuite )
        return NULL;

    if ( ( NULL == CU_add_test ( pSuite, "of gsb_parse_date_string()", utils_dates_cunit__gsb_parse_date_string ) )
       )
        return NULL;

    return pSuite;
}