~drizzle-trunk/drizzle/development

1377.8.2 by Paweł Blokus
created files for temporal tests
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
21
#include "config.h"
22
1377.8.23 by Paweł Blokus
added generator for TemporalFormat
23
#include <drizzled/temporal.h>
24
#include <drizzled/temporal_format.h>
25
#include <gtest/gtest.h>
1377.8.29 by Paweł Blokus
tests for init/deinit_temporal_formats
26
#include <vector>
1377.8.23 by Paweł Blokus
added generator for TemporalFormat
27
1377.9.1 by Paweł Blokus
updated includes to last file rename
28
#include "temporal_generator.h"
1377.8.23 by Paweł Blokus
added generator for TemporalFormat
29
30
using namespace drizzled;
31
32
class TemporalFormatTest : public ::testing::Test
33
{
34
  protected:
35
    TemporalFormat *tf;
36
    Temporal *temporal;
37
    bool result;
38
39
  virtual void SetUp()
40
  {
41
    tf= NULL;
42
    temporal= NULL;
43
  }
44
45
  virtual void TearDown()
46
  {
47
    if (tf != NULL)
48
      delete tf;
49
    if (temporal != NULL)
50
      delete temporal;
51
  }
52
};
53
54
55
TEST_F(TemporalFormatTest, constructor_WithValidRegExp_shouldCompile)
56
{
57
  tf= new TemporalFormat("^(\\d{4})[-/.]$");
58
  
59
  result= tf->is_valid();
60
61
  ASSERT_TRUE(result);
62
}
63
64
TEST_F(TemporalFormatTest, constructor_WithInvalidRegExp_shouldNotCompile)
65
{
1377.8.25 by Paweł Blokus
this was the first working build, and these changes make almost all the tests pass
66
  tf= new TemporalFormat("^\\d{4)[-/.]$");
1377.8.23 by Paweł Blokus
added generator for TemporalFormat
67
68
  result= tf->is_valid();
69
  
70
  ASSERT_FALSE(result);
71
}
72
73
TEST_F(TemporalFormatTest, matches_OnNotMatchingString_shouldReturn_False)
74
{
1377.8.25 by Paweł Blokus
this was the first working build, and these changes make almost all the tests pass
75
  tf= new TemporalFormat("^(\\d{4})[-/.]$");
1377.8.23 by Paweł Blokus
added generator for TemporalFormat
76
  char matched[] ="1234/ABC";
77
78
  result= tf->matches(matched, sizeof(matched) - sizeof(char), NULL);
79
  
80
  ASSERT_FALSE(result);
81
}
82
83
TEST_F(TemporalFormatTest, matches_OnMatchingString_FormatWithIndexesSet_shouldPopulateTemporal)
84
{
1377.8.35 by Paweł Blokus
fixed some style issues and decided on buffer size in temporal_interval_test
85
  char regexp[]= "^(\\d{4})[-/.](\\d{1,2})[-/.](\\d{1,2})[T|\\s+](\\d{2}):(\\d{2}):(\\d{2})$";
1377.8.23 by Paweł Blokus
added generator for TemporalFormat
86
  char matched[]= "1999/9/14T23:29:05";
1377.8.36 by Paweł Blokus
changed Generator to TemporalGenerator
87
  tf= TemporalGenerator::TemporalFormatGen::make_temporal_format(regexp, 1, 2, 3, 4, 5, 6, 0, 0);
1377.8.23 by Paweł Blokus
added generator for TemporalFormat
88
  temporal= new DateTime();
89
90
  
1377.8.35 by Paweł Blokus
fixed some style issues and decided on buffer size in temporal_interval_test
91
  result= tf->matches(matched, sizeof(matched) - sizeof(char), temporal);
1377.8.25 by Paweł Blokus
this was the first working build, and these changes make almost all the tests pass
92
  ASSERT_TRUE(result);
1377.8.23 by Paweł Blokus
added generator for TemporalFormat
93
  
94
  EXPECT_EQ(1999, temporal->years());
95
  EXPECT_EQ(9, temporal->months());
96
  EXPECT_EQ(14, temporal->days());
97
  EXPECT_EQ(23, temporal->hours());
98
  EXPECT_EQ(29, temporal->minutes());
99
  EXPECT_EQ(5, temporal->seconds());
100
}
101
102
TEST_F(TemporalFormatTest, matches_FormatWithMicroSecondIndexSet_shouldAddTrailingZeros)
103
{
1377.8.36 by Paweł Blokus
changed Generator to TemporalGenerator
104
  tf= TemporalGenerator::TemporalFormatGen::make_temporal_format("^(\\d{1,6})$", 0, 0, 0, 0, 0, 0, 1, 0);
1377.8.23 by Paweł Blokus
added generator for TemporalFormat
105
  char matched[]= "560";
106
  temporal= new Time();
107
  
108
  tf->matches(matched, sizeof(matched) - sizeof(char), temporal);
109
  
110
  ASSERT_EQ(560000, temporal->useconds());
111
}
112
113
TEST_F(TemporalFormatTest, matches_FormatWithNanoSecondIndexSet_shouldAddTrailingZeros)
114
{
1377.8.36 by Paweł Blokus
changed Generator to TemporalGenerator
115
  tf= TemporalGenerator::TemporalFormatGen::make_temporal_format("^(\\d{1,9})$", 0, 0, 0, 0, 0, 0, 0, 1);
1377.8.23 by Paweł Blokus
added generator for TemporalFormat
116
  char matched[]= "4321";
117
  temporal= new Time();
118
  
119
  tf->matches(matched, sizeof(matched) - sizeof(char), temporal);
120
  
121
  ASSERT_EQ(432100000, temporal->nseconds());
122
}
1377.8.29 by Paweł Blokus
tests for init/deinit_temporal_formats
123
1377.8.31 by Paweł Blokus
little fix in include.am
124
namespace drizzled
125
{
1377.8.29 by Paweł Blokus
tests for init/deinit_temporal_formats
126
extern std::vector<TemporalFormat *> known_datetime_formats;
127
extern std::vector<TemporalFormat *> known_date_formats;
128
extern std::vector<TemporalFormat *> known_time_formats;
129
extern std::vector<TemporalFormat *> all_temporal_formats;
1377.8.31 by Paweł Blokus
little fix in include.am
130
}
1377.8.29 by Paweł Blokus
tests for init/deinit_temporal_formats
131
132
TEST(TemporalFormatInitTest, init_temporal_formats_vectorsWithKnownFormats_shouldHaveExpectedLengths)
133
{
134
  init_temporal_formats();
135
1377.8.31 by Paweł Blokus
little fix in include.am
136
  EXPECT_EQ(13, known_datetime_formats.size());	
1377.8.29 by Paweł Blokus
tests for init/deinit_temporal_formats
137
  EXPECT_EQ(8, known_date_formats.size());
138
  EXPECT_EQ(6, known_time_formats.size());
139
  EXPECT_EQ(19, all_temporal_formats.size());
140
}
141
142
TEST(TemporalFormatDeinitTest, deinit_temporal_formats_vectorsWithKnownFormats_shouldHaveZeroLengths)
143
{
144
  init_temporal_formats();
145
  deinit_temporal_formats();
146
  
147
  EXPECT_EQ(0, known_datetime_formats.size());
148
  EXPECT_EQ(0, known_date_formats.size());
149
  EXPECT_EQ(0, known_time_formats.size());
150
  EXPECT_EQ(0, all_temporal_formats.size());
151
}