This repository has been archived by the owner on Sep 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 623
/
decimal_functions_sql_test.cpp
81 lines (71 loc) · 2.63 KB
/
decimal_functions_sql_test.cpp
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
//===----------------------------------------------------------------------===//
//
// Peloton
//
// distinct_sql_test.cpp
//
// Identification: test/sql/distinct_sql_test.cpp
//
// Copyright (c) 2015-17, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//
#include <memory>
#include "sql/testing_sql_util.h"
#include "catalog/catalog.h"
#include "common/harness.h"
#include "concurrency/transaction_manager_factory.h"
namespace peloton {
namespace test {
class DecimalFunctionsSQLTest : public PelotonTest {};
TEST_F(DecimalFunctionsSQLTest, FloorTest) {
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
auto txn = txn_manager.BeginTransaction();
catalog::Catalog::GetInstance()->CreateDatabase(DEFAULT_DB_NAME, txn);
catalog::Catalog::GetInstance()->Bootstrap();
txn_manager.CommitTransaction(txn);
// Create a t
txn = txn_manager.BeginTransaction();
TestingSQLUtil::ExecuteSQLQuery(
"CREATE TABLE foo(id integer, income decimal);");
// Adding in 2500 random decimal inputs between [-500, 500]
int i;
std::vector<double> inputs;
int lo = -500;
int hi = 500;
int numEntries = 500;
// Setting a seed
std::srand(std::time(0));
for (i = 0; i < numEntries; i++) {
double num = 0.45 + (std::rand() % (hi - lo));
inputs.push_back(num);
std::ostringstream os;
os << "insert into foo values(" << i << ", " << num << ");";
TestingSQLUtil::ExecuteSQLQuery(os.str());
}
EXPECT_EQ(i, numEntries);
// Fetch values from the table
std::vector<StatementResult> result;
std::vector<FieldInfo> tuple_descriptor;
std::string error_message;
int rows_affected;
std::ostringstream os;
os << "select id, floor(income) from foo;";
TestingSQLUtil::ExecuteSQLQuery(os.str(), result, tuple_descriptor,
rows_affected, error_message);
for (i = 0; i < numEntries; i++) {
std::string result_id(
TestingSQLUtil::GetResultValueAsString(result, (2 * i)));
std::string result_income(
TestingSQLUtil::GetResultValueAsString(result, (2 * i) + 1));
int id = std::stoi(result_id);
double income = std::stod(result_income);
EXPECT_EQ(id, i);
EXPECT_DOUBLE_EQ(income, floor(inputs[i]));
}
// free the database just created
txn = txn_manager.BeginTransaction();
catalog::Catalog::GetInstance()->DropDatabaseWithName(DEFAULT_DB_NAME, txn);
txn_manager.CommitTransaction(txn);
}
} // namespace test
} // namespace peloton