Skip to content

Commit da77cdc

Browse files
author
Kimmo Vaisanen
committed
UT for CellularDevice get/set timeouts methods
1 parent ce3d414 commit da77cdc

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

UNITTESTS/features/cellular/framework/device/cellulardevice/cellulardevicetest.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,35 @@ TEST_F(TestCellularDevice, test_shutdown)
240240

241241
delete dev;
242242
}
243+
244+
TEST_F(TestCellularDevice, test_timeout_array)
245+
{
246+
FileHandle_stub fh1;
247+
myCellularDevice *dev = new myCellularDevice(&fh1);
248+
EXPECT_TRUE(dev);
249+
250+
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK;
251+
252+
// Max size
253+
uint16_t set_timeouts[CELLULAR_RETRY_ARRAY_SIZE + 1];
254+
for (int i = 0; i < CELLULAR_RETRY_ARRAY_SIZE; i++) {
255+
set_timeouts[i] = i + 100;
256+
}
257+
dev->set_retry_timeout_array(set_timeouts, CELLULAR_RETRY_ARRAY_SIZE);
258+
259+
uint16_t verify_timeouts[CELLULAR_RETRY_ARRAY_SIZE + 1];
260+
for (int i = 0; i < CELLULAR_RETRY_ARRAY_SIZE; i++) {
261+
verify_timeouts[i] = i + 100;
262+
}
263+
dev->verify_timeout_array(verify_timeouts, CELLULAR_RETRY_ARRAY_SIZE);
264+
265+
// Empty
266+
dev->set_retry_timeout_array(NULL, 0);
267+
dev->verify_timeout_array(NULL, 0);
268+
269+
// Oversize (returns only CELLULAR_RETRY_ARRAY_SIZE)
270+
dev->set_retry_timeout_array(set_timeouts, CELLULAR_RETRY_ARRAY_SIZE + 1);
271+
dev->verify_timeout_array(verify_timeouts, CELLULAR_RETRY_ARRAY_SIZE);
272+
273+
delete dev;
274+
}

UNITTESTS/stubs/CellularStateMachine_stub.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ nsapi_error_t CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK;
2424
CellularStubState CellularStateMachine_stub::get_current_target_state = STATE_INIT;
2525
CellularStubState CellularStateMachine_stub::get_current_current_state = STATE_INIT;
2626
bool CellularStateMachine_stub::bool_value = false;
27+
std::vector<uint16_t> CellularStateMachine_stub::timeouts;
2728

2829
CellularStateMachine::CellularStateMachine(CellularDevice &device, events::EventQueue &queue, CellularNetwork &nw) :
2930
_cellularDevice(device), _network(nw), _queue(queue)
3031
{
32+
CellularStateMachine_stub::timeouts.clear();
3133
}
3234

3335
CellularStateMachine::~CellularStateMachine()
@@ -77,11 +79,21 @@ void CellularStateMachine::cellular_event_changed(nsapi_event_t ev, intptr_t ptr
7779

7880
void CellularStateMachine::get_retry_timeout_array(uint16_t *timeout, int &array_len) const
7981
{
80-
82+
const int array_size = CellularStateMachine_stub::timeouts.size();
83+
for (int i = 0; i < array_size; i++) {
84+
timeout[i] = CellularStateMachine_stub::timeouts[i];
85+
}
86+
array_len = array_size;
8187
}
8288

8389
void CellularStateMachine::set_retry_timeout_array(const uint16_t timeout[], int array_len)
8490
{
91+
CellularStateMachine_stub::timeouts.clear();
92+
93+
const int real_size = array_len > CELLULAR_RETRY_ARRAY_SIZE ? CELLULAR_RETRY_ARRAY_SIZE : array_len;
94+
for (int i = 0; i < real_size; i++) {
95+
CellularStateMachine_stub::timeouts.push_back(timeout[i]);
96+
}
8597
}
8698

8799
void CellularStateMachine::set_timeout(int timeout)

UNITTESTS/stubs/CellularStateMachine_stub.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define CELLULARSTATEMACHINE_STUB_H_
1919

2020
#include "CellularStateMachine.h"
21+
#include <vector>
2122

2223
enum CellularStubState {
2324
STATE_INIT = 0,
@@ -35,6 +36,7 @@ extern nsapi_error_t nsapi_error_value;
3536
extern CellularStubState get_current_target_state;
3637
extern CellularStubState get_current_current_state;
3738
extern bool bool_value;
39+
extern std::vector<uint16_t> timeouts;
3840
}
3941

4042

UNITTESTS/target_h/myCellularDevice.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "FileHandle_stub.h"
2323
#include "ATHandler_stub.h"
2424
#include "AT_CellularContext.h"
25+
#include "gtest/gtest.h"
2526

2627
using namespace events;
2728

@@ -175,6 +176,24 @@ class myCellularDevice : public CellularDevice {
175176
{
176177
return NSAPI_ERROR_OK;
177178
}
179+
180+
void verify_timeout_array(const uint16_t timeout[], int array_len)
181+
{
182+
if (array_len > CELLULAR_RETRY_ARRAY_SIZE) {
183+
FAIL();
184+
}
185+
uint16_t get_timeouts[CELLULAR_RETRY_ARRAY_SIZE];
186+
int get_timeouts_len = 0;
187+
188+
get_retry_timeout_array(get_timeouts, get_timeouts_len);
189+
190+
EXPECT_EQ(array_len, get_timeouts_len);
191+
192+
for (int i = 0; i < array_len; i++) {
193+
EXPECT_EQ(timeout[i], get_timeouts[i]);
194+
}
195+
}
196+
178197
AT_CellularNetwork *_network;
179198
AT_CellularContext *_context_list;
180199
};

0 commit comments

Comments
 (0)