tizen 2.3.1 release
[framework/web/wearable/wrt-commons.git] / tests / test / test_value_separated_reader.cpp
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        test_value_separated_reader.cpp
18  * @author      Tomasz Iwanek (t.iwanek@samsung.com)
19  * @brief       tests for VSReader
20  */
21
22 #include<dpl/test/value_separated_reader.h>
23 #include<dpl/abstract_input.h>
24 #include<dpl/test/test_runner.h>
25
26 #include<string>
27
28 using namespace  DPL;
29
30 RUNNER_TEST_GROUP_INIT(ValueSeparatedReader)
31
32 RUNNER_TEST(ValueSeparatedReader_readValidCSV)
33 {
34     std::string data;
35     data += "1-1,1-2,1-3,1-4\n";
36     data += "2-1,2-2,2-3,2-4\n";
37     data += "3-1,3-2,3-3,3-4\n";
38     data += "4-1,4-2,4-3,4-4\n";
39     std::shared_ptr<AbstractInput> mem(new BinaryQueue());
40     dynamic_cast<BinaryQueue*>(mem.get())->AppendCopy(data.data(), data.size());
41     CSVReader csv(mem);
42     VSResultPtr result = csv.ReadInput();
43     RUNNER_ASSERT_MSG((*result)[0][0] == "1-1", "Wrong value");
44     RUNNER_ASSERT_MSG((*result)[0][1] == "1-2", "Wrong value");
45     RUNNER_ASSERT_MSG((*result)[0][2] == "1-3", "Wrong value");
46     RUNNER_ASSERT_MSG((*result)[0][3] == "1-4", "Wrong value");
47
48     RUNNER_ASSERT_MSG((*result)[1][0] == "2-1", "Wrong value");
49     RUNNER_ASSERT_MSG((*result)[1][1] == "2-2", "Wrong value");
50     RUNNER_ASSERT_MSG((*result)[1][2] == "2-3", "Wrong value");
51     RUNNER_ASSERT_MSG((*result)[1][3] == "2-4", "Wrong value");
52
53     RUNNER_ASSERT_MSG((*result)[2][0] == "3-1", "Wrong value");
54     RUNNER_ASSERT_MSG((*result)[2][1] == "3-2", "Wrong value");
55     RUNNER_ASSERT_MSG((*result)[2][2] == "3-3", "Wrong value");
56     RUNNER_ASSERT_MSG((*result)[2][3] == "3-4", "Wrong value");
57
58     RUNNER_ASSERT_MSG((*result)[3][0] == "4-1", "Wrong value");
59     RUNNER_ASSERT_MSG((*result)[3][1] == "4-2", "Wrong value");
60     RUNNER_ASSERT_MSG((*result)[3][2] == "4-3", "Wrong value");
61     RUNNER_ASSERT_MSG((*result)[3][3] == "4-4", "Wrong value");
62 }
63
64 RUNNER_TEST(ValueSeparatedReader_readInvalidCSV)
65 {
66     Try
67     {
68         std::string data;
69         data += "1-1,1-2,1-3,1-4\n";
70         data += "2-1,2-2,2-3,2-4\n";
71         data += "3-1,3-2,3-3\n";
72         data += "4-1,4-2,4-3,4-4\n";
73         std::shared_ptr<AbstractInput> mem(new BinaryQueue());
74         dynamic_cast<BinaryQueue*>(mem.get())->AppendCopy(data.data(), data.size());
75         CSVReader csv(mem);
76         VSResultPtr result = csv.ReadInput();
77     }
78     Catch(CSVReader::Exception::ParserError)
79     {
80         return;
81     }
82     RUNNER_ASSERT_MSG(false, "Should throw parser error");
83 }