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 validBooleanInstances = Config{&Config::instances, 46 // Default for integers is unsigned/32bits 47 R"(<BooleanParameter Name="Empty"/> 48 <BooleanParameter Name="nominal"/>)"}; 49 50 struct BooleanPF : public ParameterFramework 51 { BooleanPFparameterFramework::BooleanPF52 BooleanPF() : ParameterFramework{std::move(validBooleanInstances)} {} 53 }; 54 55 SCENARIO_METHOD(BooleanPF, "Boolean types", "[Boolean types]") 56 { 57 GIVEN ("A valid XML structure file") { 58 THEN ("Start should succeed") { 59 CHECK_NOTHROW(start()); 60 REQUIRE_NOTHROW(setTuningMode(true)); 61 string path = "/test/test/nominal"; 62 63 AND_THEN ("Set/Get a Boolean type parameter in real value space") { 64 65 for (auto &vec : Tests<string>{ 66 {"(too high)", "2"}, {"(too low)", "-1"}, {"(not a number)", "foobar"}, 67 }) { 68 GIVEN ("Invalid value " + vec.title) { 69 CHECK_THROWS_AS(setParameter(path, vec.payload), Exception); 70 } 71 } 72 for (auto &vec : Tests<string>{ 73 {"(upper limit)", "1"}, {"(lower limit)", "0"}, 74 }) { 75 GIVEN ("A valid value " + vec.title) { 76 CHECK_NOTHROW(setParameter(path, vec.payload)); 77 string getValueBack; 78 REQUIRE_NOTHROW(getParameter(path, getValueBack)); 79 CHECK(getValueBack == vec.payload); 80 } 81 } 82 } 83 84 AND_THEN ("Set/Get boolean type parameter handle") { 85 ElementHandle handle{*this, path}; 86 /** @FIXME: 'set' operations on a ParameterHandle are silently 87 * ignored in tuning mode. Does it make sense ? */ 88 REQUIRE_NOTHROW(setTuningMode(false)); 89 90 for (auto &vec : Tests<bool>{ 91 {"(upper limit)", true}, {"(lower limit)", false}, 92 }) { 93 GIVEN ("A valid value " + vec.title) { 94 CHECK_NOTHROW(handle.setAsBoolean(vec.payload)); 95 bool getValueBack; 96 REQUIRE_NOTHROW(handle.getAsBoolean(getValueBack)); 97 CHECK(getValueBack == vec.payload); 98 } 99 } 100 } 101 102 AND_THEN ("Set/Get integer type parameter handle") { 103 ElementHandle handle{*this, path}; 104 /** @FIXME: 'set' operations on a ParameterHandle are silently 105 * ignored in tuning mode. Does it make sense ? */ 106 REQUIRE_NOTHROW(setTuningMode(false)); 107 108 for (auto &vec : Tests<uint32_t>{ 109 {"(upper limit)", 1}, {"(lower limit)", 0}, 110 }) { 111 GIVEN ("A valid value " + vec.title) { 112 CHECK_NOTHROW(handle.setAsInteger(vec.payload)); 113 uint32_t getValueBack; 114 REQUIRE_NOTHROW(handle.getAsInteger(getValueBack)); 115 CHECK(getValueBack == vec.payload); 116 } 117 } 118 for (auto &vec : Tests<uint32_t>{ 119 {"(too high)", 2}, 120 }) { 121 GIVEN ("An invalid value " + vec.title) { 122 CHECK_THROWS_AS(handle.setAsInteger(vec.payload), Exception); 123 } 124 } 125 } 126 127 AND_THEN ("Set/Get a Boolean type parameter in real value space") { 128 ElementHandle handle{*this, path}; 129 REQUIRE_NOTHROW(setRawValueSpace(true)); 130 131 for (auto &vec : Tests<string>{ 132 {"(too high hexa)", "0x2"}, 133 {"(too high dec)", "2"}, 134 {"(too low hexa )", "0xFF"}, 135 {"(not a number)", "foobar"}, 136 }) { 137 GIVEN ("Invalid value " + vec.title) { 138 CHECK_THROWS_AS(setParameter(path, vec.payload), Exception); 139 } 140 } 141 for (auto &vec : Tests<string>{ 142 {"(TRUE hexa)", "0x1"}, {"(TRUE dec)", "1"}, 143 }) { 144 GIVEN ("A valid value " + vec.title) { 145 CHECK_NOTHROW(setParameter(path, vec.payload)); 146 string getValueBack; 147 REQUIRE_NOTHROW(getParameter(path, getValueBack)); 148 CHECK(getValueBack == "1"); 149 } 150 } 151 for (auto &vec : Tests<string>{ 152 {"(FALSE hexa)", "0x0"}, {"(FALSE dec)", "0"}, 153 }) { 154 GIVEN ("A valid value " + vec.title) { 155 CHECK_NOTHROW(setParameter(path, vec.payload)); 156 string getValueBack; 157 REQUIRE_NOTHROW(getParameter(path, getValueBack)); 158 CHECK(getValueBack == "0"); 159 } 160 } 161 } 162 } 163 } 164 } 165 } 166