~drizzle-trunk/drizzle/development

2097.2.4 by Andrew Hutchings
Convert all unit tests to boost::test
1
/* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2010 Pawel Blokus
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 */
20
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
21
#include <config.h>
2097.2.4 by Andrew Hutchings
Convert all unit tests to boost::test
22
23
#include <drizzled/temporal.h>
24
#include <drizzled/temporal_format.h>
25
26
#define BOOST_TEST_DYN_LINK
27
#include <boost/test/unit_test.hpp>
28
29
#include <vector>
30
31
#include "temporal_generator.h"
32
33
using namespace drizzled;
34
35
class TemporalFormatTest
36
{
37
  protected:
38
    TemporalFormat *tf;
39
    Temporal *temporal;
40
    bool result;
41
42
  TemporalFormatTest()
43
  {
44
    tf= NULL;
45
    temporal= NULL;
46
  }
47
48
  ~TemporalFormatTest()
49
  {
2353.3.1 by Mark Atwood
fix cppcheck redundantIfDelete0 warnings. It is safe to deallocate a NULL pointer
50
    delete tf;
51
    delete temporal;
2097.2.4 by Andrew Hutchings
Convert all unit tests to boost::test
52
  }
53
};
54
55
BOOST_FIXTURE_TEST_SUITE(TemporalFormatTestSuite, TemporalFormatTest)
56
BOOST_AUTO_TEST_CASE(constructor_WithValidRegExp_shouldCompile)
57
{
58
  tf= new TemporalFormat("^(\\d{4})[-/.]$");
59
  
60
  result= tf->is_valid();
61
62
  BOOST_REQUIRE(result);
63
}
64
65
BOOST_AUTO_TEST_CASE(constructor_WithInvalidRegExp_shouldNotCompile)
66
{
67
  tf= new TemporalFormat("^\\d{4)[-/.]$");
68
69
  result= tf->is_valid();
70
  
71
  BOOST_REQUIRE(not result);
72
}
73
74
BOOST_AUTO_TEST_CASE(matches_OnNotMatchingString_shouldReturn_False)
75
{
76
  tf= new TemporalFormat("^(\\d{4})[-/.]$");
77
  char matched[] ="1234/ABC";
78
79
  result= tf->matches(matched, sizeof(matched) - sizeof(char), NULL);
80
  
81
  BOOST_REQUIRE(not result);
82
}
83
84
BOOST_AUTO_TEST_CASE(matches_OnMatchingString_FormatWithIndexesSet_shouldPopulateTemporal)
85
{
86
  char regexp[]= "^(\\d{4})[-/.](\\d{1,2})[-/.](\\d{1,2})[T|\\s+](\\d{2}):(\\d{2}):(\\d{2})$";
87
  char matched[]= "1999/9/14T23:29:05";
88
  tf= TemporalGenerator::TemporalFormatGen::make_temporal_format(regexp, 1, 2, 3, 4, 5, 6, 0, 0);
89
  temporal= new DateTime();
90
91
  
92
  result= tf->matches(matched, sizeof(matched) - sizeof(char), temporal);
93
  BOOST_REQUIRE(result);
94
  
95
  BOOST_REQUIRE_EQUAL(1999, temporal->years());
96
  BOOST_REQUIRE_EQUAL(9, temporal->months());
97
  BOOST_REQUIRE_EQUAL(14, temporal->days());
98
  BOOST_REQUIRE_EQUAL(23, temporal->hours());
99
  BOOST_REQUIRE_EQUAL(29, temporal->minutes());
100
  BOOST_REQUIRE_EQUAL(5, temporal->seconds());
101
}
102
103
BOOST_AUTO_TEST_CASE(matches_FormatWithMicroSecondIndexSet_shouldAddTrailingZeros)
104
{
105
  tf= TemporalGenerator::TemporalFormatGen::make_temporal_format("^(\\d{1,6})$", 0, 0, 0, 0, 0, 0, 1, 0);
106
  char matched[]= "560";
107
  temporal= new Time();
108
  
109
  tf->matches(matched, sizeof(matched) - sizeof(char), temporal);
110
  
111
  BOOST_REQUIRE_EQUAL(560000, temporal->useconds());
112
}
113
114
BOOST_AUTO_TEST_CASE(matches_FormatWithNanoSecondIndexSet_shouldAddTrailingZeros)
115
{
116
  tf= TemporalGenerator::TemporalFormatGen::make_temporal_format("^(\\d{1,9})$", 0, 0, 0, 0, 0, 0, 0, 1);
117
  char matched[]= "4321";
118
  temporal= new Time();
119
  
120
  tf->matches(matched, sizeof(matched) - sizeof(char), temporal);
121
  
122
  BOOST_REQUIRE_EQUAL(432100000, temporal->nseconds());
123
}
124
BOOST_AUTO_TEST_SUITE_END()
125
126
namespace drizzled
127
{
128
extern std::vector<TemporalFormat *> known_datetime_formats;
129
extern std::vector<TemporalFormat *> known_date_formats;
130
extern std::vector<TemporalFormat *> known_time_formats;
131
extern std::vector<TemporalFormat *> all_temporal_formats;
132
}
133
134
BOOST_AUTO_TEST_SUITE(TemporalFormatTestSuite)
135
BOOST_AUTO_TEST_CASE(init_temporal_formats_vectorsWithKnownFormats_shouldHaveExpectedLengths)
136
{
137
  init_temporal_formats();
138
139
  BOOST_REQUIRE_EQUAL(13, known_datetime_formats.size());	
140
  BOOST_REQUIRE_EQUAL(8, known_date_formats.size());
141
  BOOST_REQUIRE_EQUAL(11, known_time_formats.size());
142
  BOOST_REQUIRE_EQUAL(19, all_temporal_formats.size());
143
}
144
145
BOOST_AUTO_TEST_CASE(deinit_temporal_formats_vectorsWithKnownFormats_shouldHaveZeroLengths)
146
{
147
  init_temporal_formats();
148
  deinit_temporal_formats();
149
  
150
  BOOST_REQUIRE_EQUAL(0, known_datetime_formats.size());
151
  BOOST_REQUIRE_EQUAL(0, known_date_formats.size());
152
  BOOST_REQUIRE_EQUAL(0, known_time_formats.size());
153
  BOOST_REQUIRE_EQUAL(0, all_temporal_formats.size());
154
}
155
BOOST_AUTO_TEST_SUITE_END()