Initialize Tizen 2.3
[framework/web/wrt-commons.git] / tests / test / test_abstract_input_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_abstract_input_reader.h
18  * @author      Tomasz Iwanek (t.iwanek@samsung.com)
19  * @brief       tests for AbstractInputReader
20  */
21
22 #include <cstring>
23 #include <string>
24
25 #include <dpl/test/abstract_input_reader.h>
26 #include <dpl/abstract_input.h>
27 #include <dpl/test/test_runner.h>
28 #include <dpl/binary_queue.h>
29 #include <dpl/optional.h>
30
31 using namespace DPL;
32
33 namespace {
34
35 // TOKENIZER
36
37 class DigitTokenizer : public AbstractInputTokenizer<int>
38 {
39 public:
40     DigitTokenizer() : m_valid(true) {}
41
42     std::unique_ptr<int> GetNextToken()
43     {
44         typedef AbstractInputTokenizer<int>::Exception::TokenizerError TokenizerError;
45
46         std::unique_ptr<int> token;
47
48         char buffer;
49         BinaryQueueAutoPtr baptr = m_input->Read(1); //not effective but it's test...
50         if(baptr.get() == NULL)
51         {
52             ThrowMsg(TokenizerError, "Input reading failed");
53         }
54         if(baptr->Empty()) //end of source
55         {
56             return token;
57         }
58         baptr->FlattenConsume(&buffer,1);
59         if(!isdigit(buffer))
60         {
61             ThrowMsg(TokenizerError, "Input source contains no digit characters/bytes");
62         }
63         token.reset(new int(static_cast<int>(buffer)));
64         return token;
65     }
66
67     void Reset(std::shared_ptr<AbstractInput> ia)
68     {
69         AbstractInputTokenizer<int>::Reset(ia);
70         m_valid = true;
71     }
72
73     bool IsStateValid()
74     {
75         return true;
76     }
77
78 private:
79     bool m_valid;
80 };
81
82 // PARSER
83
84 class SumatorParser : public AbstractInputParser<int, int>
85 {
86 public:
87     SumatorParser() : m_sum(0) {}
88
89     void ConsumeToken(std::unique_ptr<int> && token)
90     {
91         m_sum += (*token - '0');
92     }
93
94     bool IsStateValid()
95     {
96         return true;
97     }
98
99     int GetResult() const
100     {
101         return m_sum;
102     }
103
104 private:
105     int m_sum;
106 };
107
108 // READER
109
110 class Sumator : public AbstractInputReader<int, int>
111 {
112 public:
113     Sumator(std::shared_ptr<AbstractInput> ia)
114         : AbstractInputReader<int, int>(ia,
115             std::unique_ptr<ParserBase>(new SumatorParser()),
116             std::unique_ptr<TokenizerBase>(new DigitTokenizer()))
117     {}
118 };
119
120 }
121
122 RUNNER_TEST_GROUP_INIT(AbstractInputReader)
123
124 RUNNER_TEST(AbstractInputReader_ByteSumatorInstance_Sum)
125 {
126     const std::string data("1234567890");
127     std::shared_ptr<AbstractInput> mem(new BinaryQueue());
128     dynamic_cast<BinaryQueue*>(mem.get())->AppendCopy(data.c_str(), data.size());
129     Sumator sum(mem);
130     int result = sum.ReadInput();
131     RUNNER_ASSERT_MSG(result == 45, "Sum is invalid");
132 }
133
134 RUNNER_TEST(AbstractInputReader_ByteSumatorInstance_Exception)
135 {
136     const std::string data("12345string90");
137     std::shared_ptr<AbstractInput> mem(new BinaryQueue());
138     dynamic_cast<BinaryQueue*>(mem.get())->AppendCopy(data.c_str(), data.size());
139     Sumator sum(mem);
140     Try
141     {
142         sum.ReadInput();
143     }
144     Catch(Sumator::Exception::TokenizerError)
145     {
146         return;
147     }
148     RUNNER_ASSERT_MSG(false, "Tokenizer exception should be thrown");
149 }