2 * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file test_abstract_input_reader.h
18 * @author Tomasz Iwanek (t.iwanek@samsung.com)
19 * @brief tests for AbstractInputReader
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>
36 class DigitTokenizer : public AbstractInputTokenizer<int>
39 DigitTokenizer() : m_valid(true) {}
41 std::unique_ptr<int> GetNextToken()
43 typedef AbstractInputTokenizer<int>::Exception::TokenizerError TokenizerError;
45 std::unique_ptr<int> token;
48 BinaryQueueAutoPtr baptr = m_input->Read(1); //not effective but it's test...
49 if(baptr.get() == NULL)
51 ThrowMsg(TokenizerError, "Input reading failed");
53 if(baptr->Empty()) //end of source
57 baptr->FlattenConsume(&buffer,1);
60 ThrowMsg(TokenizerError, "Input source contains no digit characters/bytes");
62 token.reset(new int(static_cast<int>(buffer)));
66 void Reset(std::shared_ptr<AbstractInput> ia)
68 AbstractInputTokenizer<int>::Reset(ia);
83 class SumatorParser : public AbstractInputParser<int, int>
86 SumatorParser() : m_sum(0) {}
88 void ConsumeToken(std::unique_ptr<int> && token)
90 m_sum += (*token - '0');
109 class Sumator : public AbstractInputReader<int, int>
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()))
121 RUNNER_TEST_GROUP_INIT(AbstractInputReader)
123 RUNNER_TEST(AbstractInputReader_ByteSumatorInstance_Sum)
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());
129 int result = sum.ReadInput();
130 RUNNER_ASSERT_MSG(result == 45, "Sum is invalid");
133 RUNNER_TEST(AbstractInputReader_ByteSumatorInstance_Exception)
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());
143 Catch(Sumator::Exception::TokenizerError)
147 RUNNER_ASSERT_MSG(false, "Tokenizer exception should be thrown");