Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MAX44000 PWM_Tone_Library nexpaq_mdk
Fork of LED_Demo by
test_m2mreporthandler.cpp
00001 /* 00002 * Copyright (c) 2015 ARM Limited. All rights reserved. 00003 * SPDX-License-Identifier: Apache-2.0 00004 * Licensed under the Apache License, Version 2.0 (the License); you may 00005 * not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an AS IS BASIS, WITHOUT 00012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #include "CppUTest/TestHarness.h" 00017 #include "test_m2mreporthandler.h" 00018 #include "m2mobservationhandler.h" 00019 #include "m2mtimer.h" 00020 #include "m2mtimerobserver.h" 00021 #include "m2mbase.h" 00022 00023 class Observer : public M2MReportObserver{ 00024 00025 public: 00026 00027 Observer() : visited(false) {} 00028 virtual ~Observer(){} 00029 void observation_to_be_sent(m2m::Vector<uint16_t>,bool){ 00030 visited = true; 00031 } 00032 bool visited; 00033 }; 00034 00035 class TimerObserver : public M2MTimerObserver 00036 { 00037 public: 00038 TimerObserver() : visited(false) {} 00039 virtual ~TimerObserver(){} 00040 00041 virtual void timer_expired(M2MTimerObserver::Type ){ 00042 visited = true; 00043 } 00044 00045 bool visited; 00046 }; 00047 00048 Test_M2MReportHandler::Test_M2MReportHandler() 00049 { 00050 _observer = new Observer(); 00051 M2MReportHandler handler(*_observer); 00052 _handler = new M2MReportHandler(*_observer); 00053 } 00054 00055 Test_M2MReportHandler::~Test_M2MReportHandler() 00056 { 00057 delete _handler; 00058 delete _observer; 00059 } 00060 00061 void Test_M2MReportHandler::test_set_under_observation() 00062 { 00063 _observer->visited = false; 00064 00065 _handler->set_under_observation(true); 00066 CHECK(false == _observer->visited); 00067 CHECK(_handler->_pmin_timer == NULL); 00068 00069 _observer->visited = false; 00070 _handler->set_under_observation(false); 00071 CHECK(false == _observer->visited); 00072 } 00073 00074 void Test_M2MReportHandler::test_parse_notification_attribute() 00075 { 00076 char* val = {"value"}; 00077 CHECK(false == _handler->parse_notification_attribute(val, M2MBase::ObjectInstance )); 00078 00079 char* val2 = {"value&eval"}; 00080 CHECK(false == _handler->parse_notification_attribute(val2, M2MBase::ObjectInstance )); 00081 00082 char* val3 = {"toolongvaluevaluevaluevaluevalue&toolongvaluevaluevaluevaluevalue"}; 00083 CHECK(false == _handler->parse_notification_attribute(val3, M2MBase::ObjectInstance )); 00084 00085 char* val_real = {"st=6&pmax=3<=1>=100"}; 00086 CHECK(true == _handler->parse_notification_attribute(val_real, M2MBase::Resource, 00087 M2MResourceInstance::INTEGER )); 00088 00089 char* val_real1 = {"a=1&pmin=2&pmax=3>=4<=5&st=6"}; 00090 CHECK(false == _handler->parse_notification_attribute(val_real1, M2MBase::Resource, 00091 M2MResourceInstance::INTEGER )); 00092 00093 char* val2_real = {"st=6<=50>=1"}; 00094 CHECK(false == _handler->parse_notification_attribute(val2_real, M2MBase::Resource, 00095 M2MResourceInstance::INTEGER )); 00096 00097 char* val3_real = {"gt=40<=5&st=6"}; 00098 CHECK(true == _handler->parse_notification_attribute(val3_real, M2MBase::Resource, 00099 M2MResourceInstance::INTEGER )); 00100 00101 _handler->set_default_values(); 00102 00103 char* val5_real = {"pmin=10"}; 00104 CHECK(true == _handler->parse_notification_attribute(val5_real, M2MBase::Resource, 00105 M2MResourceInstance::INTEGER )); 00106 00107 char* val6_real = {"pmin=100&pmax=5"}; 00108 CHECK(false == _handler->parse_notification_attribute(val6_real, M2MBase::Resource, 00109 M2MResourceInstance::INTEGER )); 00110 00111 char* val7_real = {"st=6&pmax=30<=1>=100&pmin=0"}; 00112 CHECK(true == _handler->parse_notification_attribute(val7_real, M2MBase::Resource, 00113 M2MResourceInstance::INTEGER )); 00114 00115 char* val8_real = {"pmax=30<=10>=5&pmin=1"}; 00116 CHECK(false == _handler->parse_notification_attribute(val8_real, M2MBase::Resource, 00117 M2MResourceInstance::INTEGER )); 00118 00119 // low = lt + 2 * st = 18 00120 char* val9_real = {"pmax=30<=10>=17&pmin=1&st=4"}; 00121 CHECK(false == _handler->parse_notification_attribute(val9_real, M2MBase::Resource, 00122 M2MResourceInstance::INTEGER )); 00123 00124 // low = lt + 2 * st = 18 00125 char* val10_real = {"pmax=30<=10>=19&pmin=1&st=4"}; 00126 CHECK(true == _handler->parse_notification_attribute(val10_real, M2MBase::Resource, 00127 M2MResourceInstance::INTEGER )); 00128 00129 char* val11_real = {"pmax=30&pmin=30"}; 00130 CHECK(true == _handler->parse_notification_attribute(val11_real, M2MBase::Resource, 00131 M2MResourceInstance::INTEGER )); 00132 00133 _handler->set_default_values(); 00134 00135 char* inst_real = {"st=6&pmax=3<=1>=100"}; 00136 CHECK(true == _handler->parse_notification_attribute(inst_real, M2MBase::Resource, 00137 M2MResourceInstance::INTEGER )); 00138 00139 char* inst1_real1 = {"a=1&pmin=2&pmax=3>=4<=5&st=6"}; 00140 CHECK(false == _handler->parse_notification_attribute(inst1_real1, M2MBase::Resource, 00141 M2MResourceInstance::INTEGER )); 00142 00143 char* inst2_real = {"st=6<=50>=1"}; 00144 CHECK(false == _handler->parse_notification_attribute(inst2_real, M2MBase::Resource, 00145 M2MResourceInstance::INTEGER )); 00146 00147 char* inst3_real = {"gt=40<=5&st=6"}; 00148 CHECK(true == _handler->parse_notification_attribute(inst3_real, M2MBase::Resource, 00149 M2MResourceInstance::INTEGER )); 00150 00151 _handler->set_default_values(); 00152 char* inst5_real = {"pmin=10"}; 00153 CHECK(true == _handler->parse_notification_attribute(inst5_real, M2MBase::Resource, 00154 M2MResourceInstance::INTEGER )); 00155 00156 char* inst6_real = {"pmin=100&pmax=5"}; 00157 CHECK(false == _handler->parse_notification_attribute(inst6_real, M2MBase::Resource, 00158 M2MResourceInstance::INTEGER )); 00159 00160 char* inst7_real = {"st=6&pmax=30<=1>=100&pmin=0"}; 00161 CHECK(true == _handler->parse_notification_attribute(inst7_real, M2MBase::Resource, 00162 M2MResourceInstance::INTEGER )); 00163 00164 char* inst8_real = {"pmax=30<=10>=5&pmin=1"}; 00165 CHECK(false == _handler->parse_notification_attribute(inst8_real, M2MBase::Resource, 00166 M2MResourceInstance::INTEGER )); 00167 00168 // low = lt + 2 * st = 18 00169 char* inst9_real = {"pmax=30<=10>=17&pmin=1&st=4"}; 00170 CHECK(false == _handler->parse_notification_attribute(inst9_real, M2MBase::Resource, 00171 M2MResourceInstance::INTEGER )); 00172 00173 // low = lt + 2 * st = 18 00174 char* inst10_real = {"pmax=30<=10>=19&pmin=1&st=4"}; 00175 CHECK(true == _handler->parse_notification_attribute(inst10_real, M2MBase::Resource, 00176 M2MResourceInstance::INTEGER )); 00177 00178 char* inst11_real = {"pmax=30&pmin=30"}; 00179 CHECK(true == _handler->parse_notification_attribute(inst11_real, M2MBase::Resource, 00180 M2MResourceInstance::INTEGER )); 00181 00182 char* inst12_real = {"pmax=&pmin=30"}; 00183 CHECK(false == _handler->parse_notification_attribute(inst12_real, M2MBase::Resource, 00184 M2MResourceInstance::INTEGER )); 00185 char* inst13_real = {"st="}; 00186 CHECK(false == _handler->parse_notification_attribute(inst13_real, M2MBase::Resource, 00187 M2MResourceInstance::INTEGER )); 00188 00189 _handler->set_default_values(); 00190 00191 char* inst14_real = {"stp=10"}; 00192 CHECK(true == _handler->parse_notification_attribute(inst14_real, M2MBase::Resource, 00193 M2MResourceInstance::INTEGER )); 00194 char* inst15_real = {"stp=10&st=15"}; 00195 CHECK(true == _handler->parse_notification_attribute(inst15_real, M2MBase::Resource, 00196 M2MResourceInstance::INTEGER )); 00197 00198 _handler->set_default_values(); 00199 00200 DOUBLES_EQUAL(0,_handler->_lt,0); 00201 DOUBLES_EQUAL(0,_handler->_gt,0); 00202 DOUBLES_EQUAL(-1,_handler->_pmax,0); 00203 DOUBLES_EQUAL(1,_handler->_pmin,0); 00204 DOUBLES_EQUAL(0,_handler->_st,0); 00205 DOUBLES_EQUAL(0,_handler->_high_step,0); 00206 DOUBLES_EQUAL(0,_handler->_low_step,0); 00207 DOUBLES_EQUAL(-1,_handler->_last_value,0); 00208 DOUBLES_EQUAL(0,_handler->_attribute_state,0); 00209 CHECK_FALSE(_handler->_pmin_exceeded); 00210 CHECK_FALSE(_handler->_pmax_exceeded); 00211 } 00212 00213 void Test_M2MReportHandler::test_timer_expired() 00214 { 00215 _handler->timer_expired(M2MTimerObserver::Notdefined); 00216 CHECK(_observer->visited == false); 00217 00218 _handler->_notify = true; 00219 _handler->_pmin_exceeded = true; 00220 _handler->timer_expired(M2MTimerObserver::PMaxTimer); 00221 CHECK(_observer->visited == true); 00222 00223 _handler->_pmin_exceeded = false; 00224 _handler->_notify = false; 00225 _handler->_attribute_state = M2MReportHandler::Pmax; 00226 _handler->_current_value = 100; 00227 _handler->timer_expired(M2MTimerObserver::PMinTimer); 00228 CHECK(_handler->_pmin_exceeded == true); 00229 00230 _observer->visited = false; 00231 _handler->_notify = true; 00232 _handler->timer_expired(M2MTimerObserver::PMinTimer); 00233 CHECK(_observer->visited == true); 00234 00235 _handler->_notify = true; 00236 _handler->_pmin_exceeded = true; 00237 _handler->timer_expired(M2MTimerObserver::PMinTimer); 00238 CHECK(_handler->_pmin_exceeded == true); 00239 00240 _handler->_notify = true; 00241 _handler->_pmin_exceeded = false; 00242 _handler->_attribute_state = M2MReportHandler::Pmax; 00243 _handler->timer_expired(M2MTimerObserver::PMaxTimer); 00244 CHECK(_handler->_pmax_exceeded == true); 00245 } 00246 00247 void Test_M2MReportHandler::test_set_value() 00248 { 00249 _handler->_notify = true; 00250 _handler->_pmin_exceeded = false; 00251 _observer->visited = false; 00252 00253 _handler->set_value(1); 00254 _handler->set_value(10); 00255 CHECK(_observer->visited == true); 00256 00257 char* query = {"st=6"}; 00258 _handler->_attribute_state = 0; 00259 CHECK(true == _handler->parse_notification_attribute(query, 00260 M2MBase::Resource, 00261 M2MResourceInstance::INTEGER)); 00262 _observer->visited = false; 00263 _handler->set_value(15); 00264 CHECK(_observer->visited == false); 00265 00266 _observer->visited = false; 00267 _handler->set_value(21); 00268 CHECK(_observer->visited == true); 00269 00270 _handler->set_value(10); 00271 char* query2 = {"st=3<=10>=100"}; 00272 _handler->_attribute_state = 0; 00273 CHECK(true == _handler->parse_notification_attribute(query2, 00274 M2MBase::Resource, 00275 M2MResourceInstance::INTEGER)); 00276 00277 _observer->visited = false; 00278 _handler->set_value(12); 00279 CHECK(_observer->visited == false); 00280 00281 _observer->visited = false; 00282 _handler->set_value(15); 00283 CHECK(_observer->visited == true); 00284 00285 _observer->visited = false; 00286 _handler->set_value(5); 00287 CHECK(_observer->visited == true); 00288 00289 _observer->visited = false; 00290 _handler->set_value(4); 00291 CHECK(_observer->visited == true); 00292 00293 _observer->visited = false; 00294 _handler->set_value(101); 00295 CHECK(_observer->visited == true); 00296 00297 _observer->visited = false; 00298 _handler->set_value(102); 00299 CHECK(_observer->visited == true); 00300 00301 char* query3 = {"lt=10"}; 00302 _handler->set_default_values(); 00303 CHECK(true == _handler->parse_notification_attribute(query3, M2MBase::Resource, M2MResourceInstance::INTEGER)); 00304 _observer->visited = false; 00305 _handler->set_value(9); 00306 CHECK(_observer->visited == true); 00307 00308 _observer->visited = false; 00309 _handler->set_value(15); 00310 CHECK(_observer->visited == false); 00311 00312 char* query4 = {"gt=10"}; 00313 _handler->set_default_values(); 00314 CHECK(true == _handler->parse_notification_attribute(query4, M2MBase::Resource, M2MResourceInstance::INTEGER)); 00315 00316 // Instantiate timers 00317 _handler->_attribute_state |= M2MReportHandler::Pmin; 00318 _handler->handle_timers(); 00319 _observer->visited = false; 00320 _handler->set_value(9); 00321 CHECK(_observer->visited == false); 00322 // Stop timers and reset flag to previous state 00323 _handler->stop_timers(); 00324 _handler->_attribute_state = M2MReportHandler::Gt; 00325 00326 _observer->visited = false; 00327 _handler->set_value(15); 00328 CHECK(_observer->visited == true); 00329 00330 _observer->visited = false; 00331 _handler->set_value(16); 00332 CHECK(_observer->visited == true); 00333 00334 _observer->visited = false; 00335 char* query5 = {"gt=10"}; 00336 _handler->set_default_values(); 00337 CHECK(false == _handler->parse_notification_attribute(query5, M2MBase::Resource, M2MResourceInstance::STRING)); 00338 00339 _observer->visited = false; 00340 char* query6 = {"pmin=10"}; 00341 _handler->set_default_values(); 00342 CHECK(true == _handler->parse_notification_attribute(query6, M2MBase::Resource, M2MResourceInstance::STRING)); 00343 00344 _observer->visited = false; 00345 char* query7 = {"pmin=10&pmax=20"}; 00346 _handler->set_default_values(); 00347 CHECK(true == _handler->parse_notification_attribute(query7, M2MBase::Resource, M2MResourceInstance::OPAQUE)); 00348 00349 _observer->visited = false; 00350 _handler->handle_timers(); 00351 _handler->set_value(26); 00352 _handler->_pmin_exceeded = true; 00353 _handler->timer_expired(M2MTimerObserver::PMaxTimer); 00354 CHECK(_observer->visited == true); 00355 } 00356 00357 void Test_M2MReportHandler::test_trigger_object_notification() 00358 { 00359 _handler->_notify = true; 00360 _handler->_pmin_exceeded = true; 00361 _handler->set_notification_trigger(); 00362 CHECK(_handler->_pmin_exceeded == false); 00363 CHECK(_observer->visited == true); 00364 00365 _handler->_changed_instance_ids.push_back(0); 00366 _handler->_changed_instance_ids.push_back(1); 00367 _handler->_changed_instance_ids.push_back(2); 00368 00369 _handler->set_notification_trigger(); 00370 CHECK(_handler->_pmin_exceeded == false); 00371 CHECK(_observer->visited == true); 00372 00373 } 00374 00375 void Test_M2MReportHandler::test_set_string_notification_trigger() 00376 { 00377 _handler->_notify = true; 00378 _handler->_pmin_exceeded = true; 00379 _handler->set_notification_trigger(); 00380 CHECK(_handler->_pmin_exceeded == false); 00381 } 00382 00383 void Test_M2MReportHandler::test_timers() 00384 { 00385 _handler->handle_timers(); 00386 CHECK(_handler->_pmin_timer == NULL); 00387 00388 _handler->_attribute_state |= M2MReportHandler::Pmin; 00389 _handler->handle_timers(); 00390 CHECK(_handler->_pmin_timer != NULL); 00391 00392 _handler->stop_timers(); 00393 CHECK(_handler->_pmin_timer == NULL); 00394 00395 _handler->_attribute_state |= M2MReportHandler::Pmax; 00396 _handler->handle_timers(); 00397 CHECK(_handler->_pmin_timer != NULL); 00398 CHECK(_handler->_pmax_timer == NULL); 00399 00400 _handler->stop_timers(); 00401 CHECK(_handler->_pmin_timer == NULL); 00402 CHECK(_handler->_pmax_timer == NULL); 00403 00404 _handler->_pmax = 2; 00405 _handler->_pmin = 2; 00406 _handler->handle_timers(); 00407 CHECK(_handler->_pmin_timer == NULL); 00408 CHECK(_handler->_pmax_timer != NULL); 00409 CHECK(_handler->_pmin_exceeded == true); 00410 00411 _handler->stop_timers(); 00412 CHECK(_handler->_pmin_timer == NULL); 00413 CHECK(_handler->_pmax_timer == NULL); 00414 } 00415 00416 void Test_M2MReportHandler::test_attribute_flags() 00417 { 00418 CHECK(_handler->attribute_flags() == 0); 00419 _handler->_attribute_state = M2MReportHandler::Pmax | M2MReportHandler::Pmin | 00420 M2MReportHandler::St | M2MReportHandler::Gt | M2MReportHandler::Lt | M2MReportHandler::Cancel; 00421 CHECK(_handler->attribute_flags() == (1 << 6) - 1); 00422 }
Generated on Tue Jul 12 2022 12:28:54 by
