• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2015, 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 #include "BinaryCopy.hpp"
36 
37 #include <catch.hpp>
38 
39 #include <string>
40 
41 using std::string;
42 
43 namespace parameterFramework
44 {
45 
46 const auto validInstances = Config{&Config::instances,
47                                    // Size is fixed at 32 and as such is optional */
48                                    R"(<FloatingPointParameter Name="Empty"/>
49     <FloatingPointParameter Name="trivial" Size="32"/>
50     <FloatingPointParameter Name="nominal" Size="32" Min="-50.4" Max="12.2"/>
51     <FloatingPointParameter Name="defaultMin" Size="32" Max="12.2"/>
52     <FloatingPointParameter Name="defaultMax" Size="32" Min="-50.4"/>)"};
53 const auto &invalidParameters =
54     Tests<string>{{"invalid Size(64)", "<FloatingPointParameter Name='error' Size='64'/>"},
55                   {"invalid Size(16)", "<FloatingPointParameter Name='error' Size='16'/>"},
56                   {"minimum > maximum", "<FloatingPointParameter Name='error' Min='1' Max='0'/>"}};
57 
58 struct FloatsPF : public ParameterFramework
59 {
FloatsPFparameterFramework::FloatsPF60     FloatsPF() : ParameterFramework{std::move(validInstances)} {}
61 };
62 
63 SCENARIO_METHOD(LazyPF, "Invalid floating points XML structure", "[floating point]")
64 {
65     for (auto &vec : invalidParameters) {
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(FloatsPF, "Floating points", "[floating points]")
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";
82 
83             AND_THEN ("Set/Get a floating point parameter in real value space") {
84 
85                 for (auto &vec : Tests<string>{
86                          {"(too high)", "12.3"},
87                          {"(too low)", "-50.5"},
88                          {"(not a number)", "foobar"},
89                      }) {
90                     GIVEN ("Invalid value " + vec.title) {
91                         CHECK_THROWS_AS(setParameter(path, vec.payload), Exception);
92                     }
93                 }
94                 for (auto &vec : Tests<string>{
95                          {"(upper limit)", "12.2"},
96                          {"(lower limit)", "-50.4"},
97                          {"(inside range)", "0"},
98                      }) {
99                     GIVEN ("A valid value " + vec.title) {
100                         CHECK_NOTHROW(setParameter(path, vec.payload));
101                         string getValueBack;
102                         REQUIRE_NOTHROW(getParameter(path, getValueBack));
103                         CHECK(getValueBack == vec.payload);
104                     }
105                 }
106             }
107 
108             AND_THEN ("Set/Get a floating point parameter in raw value space") {
109                 const float tooHigh = 12.3f;
110                 const float tooLow = -50.5f;
111                 const float nan = std::numeric_limits<float>::quiet_NaN();
112                 const float inf = std::numeric_limits<float>::infinity();
113                 REQUIRE_NOTHROW(setRawValueSpace(true));
114                 for (auto &vec : Tests<string>{
115                          {"(too high, as decimal)",
116                           std::to_string(::utility::binaryCopy<uint32_t>(tooHigh))},
117                          {"(too low, as decimal)",
118                           std::to_string(::utility::binaryCopy<uint32_t>(tooLow))},
119                          {"(meaningless)", "foobar"},
120                          {"(infinity)", std::to_string(::utility::binaryCopy<uint32_t>(inf))},
121                          {"(NaN)", std::to_string(::utility::binaryCopy<uint32_t>(nan))},
122                      }) {
123                     GIVEN ("Invalid value " + vec.title) {
124                         CHECK_THROWS_AS(setParameter(path, vec.payload), Exception);
125                     }
126                 }
127                 const float upper = 12.2f;
128                 const float lower = -50.4f;
129                 const float zero = 0.0f;
130                 for (auto &vec : Tests<string>{
131                          {"(upper limit, as decimal)",
132                           std::to_string(::utility::binaryCopy<uint32_t>(upper))},
133                          {"(lower limit, as decimal)",
134                           std::to_string(::utility::binaryCopy<uint32_t>(lower))},
135                          {"(inside range, as decimal)",
136                           std::to_string(::utility::binaryCopy<uint32_t>(zero))},
137                      }) {
138                     GIVEN ("A valid value " + vec.title) {
139                         CHECK_NOTHROW(setParameter(path, vec.payload));
140                         string getValueBack;
141                         REQUIRE_NOTHROW(getParameter(path, getValueBack));
142                         CHECK(getValueBack == vec.payload);
143                     }
144                 }
145             }
146 
147             AND_THEN ("Set/Get floating point parameter handle") {
148                 ElementHandle handle{*this, path};
149                 /** @FIXME: 'set' operations on a ParameterHandle are silently
150                  * ignored in tuning mode. Does it make sense ? */
151                 REQUIRE_NOTHROW(setTuningMode(false));
152 
153                 /* warning: even though the API below takes a double as
154                  * argument, we need to define the test vector as floats in
155                  * order to prevent rounding issues */
156                 for (auto &vec : Tests<float>{
157                          {"(upper limit)", 12.2f},
158                          {"(lower limit)", -50.4f},
159                          {"(inside range)", 0.0f},
160                      }) {
161                     GIVEN ("A valid value " + vec.title) {
162                         CHECK_NOTHROW(handle.setAsDouble(vec.payload));
163                         double getValueBack;
164                         REQUIRE_NOTHROW(handle.getAsDouble(getValueBack));
165                         CHECK(getValueBack == vec.payload);
166                     }
167                 }
168                 for (auto &vec : Tests<float>{
169                          {"(too high)", 12.3f}, {"(too low)", -50.5f},
170                      }) {
171                     GIVEN ("An invalid value " + vec.title) {
172                         CHECK_THROWS_AS(handle.setAsDouble(vec.payload), Exception);
173                     }
174                 }
175             }
176         }
177     }
178 }
179 }
180