• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017, Intel Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors
16  * may be used to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "Config.hpp"
32 #include "ParameterFramework.hpp"
33 #include "ElementHandle.hpp"
34 #include "Test.hpp"
35 
36 #include <catch.hpp>
37 
38 #include <string>
39 
40 using std::string;
41 
42 namespace parameterFramework
43 {
44 
45 const auto validBitParameterInstances = Config{
46     &Config::instances,
47     // Default for integers is unsigned/32bits
48     R"(<BitParameterBlock Name="nominal" Size="16"><BitParameter Pos="0" Size="1" Name="bool"/><BitParameter Pos="1" Size="2" Name="twobits"/><BitParameter Pos="3" Size="3" Max="6" Name="treebits"/></BitParameterBlock>)"};
49 
50 const auto &invalidBitParameterParameters =
51     Tests<string>{{"Too much bits", "<BitParameterBlock Name='toomuchbits' Size='8'><BitParameter "
52                                     "Pos='1' Size='2' Name='twobits'/><BitParameter Pos='3' "
53                                     "Size='7' Name='toobits'/></BitParameterBlock>"},
54                   {"Max too high", "<BitParameterBlock Name='maxtoohigh' Size='8'><BitParameter "
55                                    "Pos='1' Size='2' Name='twobits'/><BitParameter Pos='3' "
56                                    "Size='2' Max='5' Name='toohigh'/></BitParameterBlock>"}};
57 
58 struct BitParameterPF : public ParameterFramework
59 {
BitParameterPFparameterFramework::BitParameterPF60     BitParameterPF() : ParameterFramework{std::move(validBitParameterInstances)} {}
61 };
62 
63 SCENARIO_METHOD(LazyPF, "Invalid BitParameter types XML structure", "[BitParameter types]")
64 {
65     for (auto &vec : invalidBitParameterParameters) {
66         GIVEN ("intentional error: " + vec.title) {
67             create(Config{&Config::instances, vec.payload});
68             THEN ("Start should fail") {
69                 CHECK_THROWS_AS(mPf->start(), Exception);
70             }
71         }
72     }
73 }
74 
75 SCENARIO_METHOD(BitParameterPF, "BitParameter types", "[BitParameter types]")
76 {
77     GIVEN ("A valid XML structure file") {
78         THEN ("Start should succeed") {
79             CHECK_NOTHROW(start());
80             REQUIRE_NOTHROW(setTuningMode(true));
81             string path = "/test/test/nominal/treebits";
82 
83             AND_THEN ("Set/Get a BitParameter type parameter in real value space") {
84 
85                 for (auto &vec : Tests<string>{
86                          {"(too high)", "7"}, {"(too low)", "-1"},
87                      }) {
88                     GIVEN ("Invalid value " + vec.title) {
89                         CHECK_THROWS_AS(setParameter(path, vec.payload), Exception);
90                     }
91                 }
92                 for (auto &vec : Tests<string>{
93                          {"(upper limit)", "6"}, {"(lower limit)", "0"},
94                      }) {
95                     GIVEN ("A valid value " + vec.title) {
96                         CHECK_NOTHROW(setParameter(path, vec.payload));
97                         string getValueBack;
98                         REQUIRE_NOTHROW(getParameter(path, getValueBack));
99                         CHECK(getValueBack == vec.payload);
100                     }
101                 }
102             }
103 
104             AND_THEN ("Set/Get a BitParameter type parameter in real value space") {
105                 ElementHandle handle{*this, path};
106                 REQUIRE_NOTHROW(setRawValueSpace(true));
107                 REQUIRE_NOTHROW(setHexOutputFormat(true));
108 
109                 for (auto &vec : Tests<string>{
110                          {"(too high)", "0x7"},
111                      }) {
112                     GIVEN ("Invalid value " + vec.title) {
113                         CHECK_THROWS_AS(setParameter(path, vec.payload), Exception);
114                     }
115                 }
116                 for (auto &vec : Tests<string>{
117                          {"(upper limit)", "0x6"}, {"(lower limit)", "0x0"},
118                      }) {
119                     GIVEN ("A valid value " + vec.title) {
120                         CHECK_NOTHROW(setParameter(path, vec.payload));
121                         string getValueBack;
122                         REQUIRE_NOTHROW(getParameter(path, getValueBack));
123                         CHECK(getValueBack == vec.payload);
124                     }
125                 }
126             }
127 
128             AND_THEN ("Set/Get a BitParameter type parameter in boolean") {
129                 ElementHandle handle{*this, path};
130                 /** @FIXME: 'set' operations on a ParameterHandle are silently
131                  * ignored in tuning mode. Does it make sense ? */
132                 REQUIRE_NOTHROW(setTuningMode(false));
133 
134                 for (auto &vec : Tests<bool>{
135                          {"(upper limit)", true}, {"(lower limit)", false},
136                      }) {
137                     GIVEN ("Invalid value " + vec.title) {
138                         CHECK_THROWS_AS(handle.setAsBoolean(vec.payload), Exception);
139                     }
140                 }
141             }
142 
143             AND_THEN ("Set/Get a BitParameter type parameter in boolean") {
144                 path = "/test/test/nominal/bool";
145                 ElementHandle handle{*this, path};
146                 /** @FIXME: 'set' operations on a ParameterHandle are silently
147                  * ignored in tuning mode. Does it make sense ? */
148                 REQUIRE_NOTHROW(setTuningMode(false));
149 
150                 for (auto &vec : Tests<bool>{
151                          {"(upper limit)", true}, {"(lower limit)", false},
152                      }) {
153                     GIVEN ("A valid value " + vec.title) {
154                         CHECK_NOTHROW(handle.setAsBoolean(vec.payload));
155                         bool getValueBack;
156                         REQUIRE_NOTHROW(handle.getAsBoolean(getValueBack));
157                         CHECK(getValueBack == vec.payload);
158                     }
159                 }
160             }
161         }
162     }
163 }
164 }
165