~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to unittests/temporal_interval_test.cc

  • Committer: Lee Bieber
  • Date: 2011-01-24 17:19:11 UTC
  • mfrom: (2097.2.8 trunk-unittests)
  • mto: This revision was merged to the branch mainline in revision 2109.
  • Revision ID: kalebral@gmail.com-20110124171911-4xztwcdk7i65osyd
Merge Andrew - fix bug 619992: fix disabled unittests 
Merge Andrew - fix bug #667162: port unittests to boost::test   

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
23
 
#include <boost/shared_ptr.hpp>
24
 
 
25
 
#include <cstdlib>
26
 
#include <gtest/gtest.h>
27
 
#include <drizzled/item.h>
28
 
#include <drizzled/session.h>
29
 
#include <drizzled/sql_string.h>
30
 
#include <drizzled/temporal_interval.h>
31
 
#include <drizzled/plugin/listen.h>
32
 
#include <drizzled/drizzled.h>
33
 
 
34
 
#include "temporal_generator.h"
35
 
 
36
 
using namespace drizzled;
37
 
 
38
 
class ItemStub : public Item
39
 
{
40
 
  ItemStub(Session *fake_session) : Item(fake_session, this)
41
 
  {
42
 
    string_to_return= NULL;
43
 
    int_to_return= 0;
44
 
  }
45
 
  
46
 
  public:
47
 
    String *string_to_return;
48
 
    int int_to_return;
49
 
    
50
 
    static boost::shared_ptr<ItemStub> get_item_stub(Session *fake_session)
51
 
    {
52
 
      boost::shared_ptr<ItemStub> result(new ItemStub(fake_session));
53
 
      return result;
54
 
    }
55
 
    
56
 
    virtual enum Type type() const { return FIELD_ITEM; };
57
 
    virtual double val_real() { return 0; };
58
 
    virtual int64_t val_int() { return int_to_return; };
59
 
    virtual String *val_str(String *str)
60
 
    {
61
 
      (void) str;
62
 
      return string_to_return;
63
 
    };
64
 
    virtual type::Decimal *val_decimal(type::Decimal *decimal_buffer)
65
 
    {
66
 
      (void) decimal_buffer;
67
 
      return NULL;
68
 
    };
69
 
};
70
 
 
71
 
class TemporalIntervalTest : public ::testing::Test
72
 
{
73
 
  protected:
74
 
    String string_to_return;
75
 
    String buffer;
76
 
    bool result;
77
 
    boost::shared_ptr<ItemStub> item;
78
 
    TemporalInterval interval;
79
 
    Session *fake_session;
80
 
 
81
 
    plugin::Client *fake_client;
82
 
    
83
 
    virtual void SetUp()
84
 
    {
85
 
      init_thread_environment();
86
 
      fake_client= plugin::Listen::getNullClient();
87
 
      fake_session= new Session(fake_client);
88
 
      fake_session->thread_stack= (char*) &fake_session;
89
 
      fake_session->initGlobals();
90
 
      item= ItemStub::get_item_stub(fake_session);
91
 
 
92
 
 
93
 
      /*28 for full date and time format with sign*/
94
 
      string_to_return.alloc(28);
95
 
      buffer.alloc(28);
96
 
      item->string_to_return= &string_to_return;
97
 
      item->int_to_return= 0;
98
 
      item->null_value= false;
99
 
    }
100
 
};
101
 
 
102
 
TEST_F(TemporalIntervalTest, initFromItem_intervalWeek)
103
 
{
104
 
  char string[]= "30";
105
 
  item->string_to_return->set_ascii(string, strlen(string));
106
 
  item->int_to_return= 30;
107
 
 
108
 
  interval.initFromItem(item.get(), INTERVAL_WEEK, &buffer);
109
 
 
110
 
  ASSERT_EQ(210, interval.get_day());
111
 
}
112
 
 
113
 
TEST_F(TemporalIntervalTest, initFromItem_intervalDayMicrosecond)
114
 
{
115
 
  char string[]= "7 12:45:19.777";
116
 
  item->string_to_return->set_ascii(string, strlen(string));
117
 
 
118
 
  interval.initFromItem(item.get(), INTERVAL_DAY_MICROSECOND, &buffer);
119
 
 
120
 
  EXPECT_EQ(7, interval.get_day());
121
 
  EXPECT_EQ(12, interval.get_hour());
122
 
  EXPECT_EQ(45, interval.get_minute());
123
 
  EXPECT_EQ(19, interval.get_second());
124
 
  EXPECT_EQ(777000, interval.get_second_part());
125
 
}
126
 
 
127
 
TEST_F(TemporalIntervalTest, initFromItem_intervalDayMicrosecond_tooFewArguments_shouldOmitHighEndItems)
128
 
{
129
 
  char string[]= "45:19.777";
130
 
  item->string_to_return->set_ascii(string, strlen(string));
131
 
  
132
 
  interval.initFromItem(item.get(), INTERVAL_DAY_MICROSECOND, &buffer);
133
 
  
134
 
  EXPECT_EQ(0, interval.get_day());
135
 
  EXPECT_EQ(0, interval.get_hour());
136
 
  EXPECT_EQ(45, interval.get_minute());
137
 
  EXPECT_EQ(19, interval.get_second());
138
 
  EXPECT_EQ(777000, interval.get_second_part());
139
 
}
140
 
 
141
 
 
142
 
TEST(TemporalIntervalAddDateTest, addDate_positiveDayMicrosecond)
143
 
{
144
 
  type::Time drizzle_time= {1990, 3, 25, 15, 5, 16, 876543, false, DRIZZLE_TIMESTAMP_DATETIME};
145
 
  TemporalInterval *interval= TemporalGenerator::TemporalIntervalGen::make_temporal_interval(
146
 
                               0, 0, 6, 13, 54, 3, 435675, false);
147
 
 
148
 
  interval->addDate(&drizzle_time, INTERVAL_DAY_MICROSECOND);
149
 
 
150
 
  EXPECT_EQ(1990, drizzle_time.year);
151
 
  EXPECT_EQ(4, drizzle_time.month);
152
 
  EXPECT_EQ(1, drizzle_time.day);
153
 
  EXPECT_EQ(4, drizzle_time.hour);
154
 
  EXPECT_EQ(59, drizzle_time.minute);
155
 
  EXPECT_EQ(20, drizzle_time.second);
156
 
  EXPECT_EQ(312218, drizzle_time.second_part);
157
 
}
158
 
 
159
 
TEST(TemporalIntervalAddDateTest, addDate_negativeDayMicrosecond)
160
 
{
161
 
  type::Time drizzle_time= {1990, 4, 1, 4, 59, 20, 312218, false, DRIZZLE_TIMESTAMP_DATETIME};
162
 
  TemporalInterval *interval= TemporalGenerator::TemporalIntervalGen::make_temporal_interval(
163
 
                               0, 0, 6, 13, 54, 3, 435675, true);
164
 
  
165
 
  interval->addDate(&drizzle_time, INTERVAL_DAY_MICROSECOND);
166
 
  
167
 
  EXPECT_EQ(1990, drizzle_time.year);
168
 
  EXPECT_EQ(3, drizzle_time.month);
169
 
  EXPECT_EQ(25, drizzle_time.day);
170
 
  EXPECT_EQ(15, drizzle_time.hour);
171
 
  EXPECT_EQ(5, drizzle_time.minute);
172
 
  EXPECT_EQ(16, drizzle_time.second);
173
 
  EXPECT_EQ(876543, drizzle_time.second_part);
174
 
}
175
 
 
176
 
TEST(TemporalIntervalAddDateTest, addDate_positiveDayMicrosecond_shouldCountLeapDayToo)
177
 
{
178
 
  type::Time drizzle_time= {2004, 2, 25, 15, 5, 16, 876543, false, DRIZZLE_TIMESTAMP_DATETIME};
179
 
  TemporalInterval *interval= TemporalGenerator::TemporalIntervalGen::make_temporal_interval(
180
 
                               0, 0, 6, 13, 54, 3, 435675, false);
181
 
  
182
 
  interval->addDate(&drizzle_time, INTERVAL_DAY_MICROSECOND);
183
 
  
184
 
  EXPECT_EQ(2004, drizzle_time.year);
185
 
  EXPECT_EQ(3, drizzle_time.month);
186
 
  EXPECT_EQ(3, drizzle_time.day);
187
 
  EXPECT_EQ(4, drizzle_time.hour);
188
 
  EXPECT_EQ(59, drizzle_time.minute);
189
 
  EXPECT_EQ(20, drizzle_time.second);
190
 
  EXPECT_EQ(312218, drizzle_time.second_part);
191
 
}
192
 
 
193
 
TEST(TemporalIntervalAddDateTest, addDate_negativeWeek)
194
 
{
195
 
  type::Time drizzle_time= {1998, 1, 25, 0, 0, 0, 0, false, DRIZZLE_TIMESTAMP_DATE};
196
 
  TemporalInterval *interval= TemporalGenerator::TemporalIntervalGen::make_temporal_interval(
197
 
                              0, 0, 28, 0, 0, 0, 0, true);
198
 
  
199
 
  interval->addDate(&drizzle_time, INTERVAL_WEEK);
200
 
  
201
 
  EXPECT_EQ(1997, drizzle_time.year);
202
 
  EXPECT_EQ(12, drizzle_time.month);
203
 
  EXPECT_EQ(28, drizzle_time.day);
204
 
}
205
 
 
206
 
TEST(TemporalIntervalAddDateTest, addDate_addPositiveYearToLeapDay)
207
 
{
208
 
  type::Time drizzle_time= {2004, 2, 29, 0, 0, 0, 0, false, DRIZZLE_TIMESTAMP_DATE};
209
 
  TemporalInterval *interval= TemporalGenerator::TemporalIntervalGen::make_temporal_interval(
210
 
                               5, 0, 0, 0, 0, 0, 0, false);
211
 
  
212
 
  interval->addDate(&drizzle_time, INTERVAL_YEAR);
213
 
  
214
 
  EXPECT_EQ(2009, drizzle_time.year);
215
 
  EXPECT_EQ(2, drizzle_time.month);
216
 
  EXPECT_EQ(28, drizzle_time.day);
217
 
}
218
 
 
219
 
TEST(TemporalIntervalAddDateTest, addDate_addOneMonthToLastDayInMonth_shouldChangeToProperLastDay)
220
 
{
221
 
  type::Time drizzle_time= {2004, 7, 31, 0, 0, 0, 0, false, DRIZZLE_TIMESTAMP_DATE};
222
 
  TemporalInterval *interval= TemporalGenerator::TemporalIntervalGen::make_temporal_interval(
223
 
                               0, 2, 0, 0, 0, 0, 0, false);
224
 
  
225
 
  interval->addDate(&drizzle_time, INTERVAL_MONTH);
226
 
  
227
 
  EXPECT_EQ(2004, drizzle_time.year);
228
 
  EXPECT_EQ(9, drizzle_time.month);
229
 
  EXPECT_EQ(30, drizzle_time.day);
230
 
}