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