GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/Blink1TestingLibrary.cpp Lines: 199 224 88.8 %
Date: 2022-10-11 07:18:13 Branches: 69 116 59.5 %

Line Branch Exec Source
1
#include <vector>
2
#include <algorithm>
3
#include <map>
4
5
#if __has_include("gtest/gtest.h")
6
    #include "gtest/gtest.h"
7
#else
8
    #include <iostream>
9
    #include <sstream>
10
    std::ostream& ADD_FAILURE() {
11
        return std::cerr;
12
    }
13
    std::stringstream dumpStream;
14
    std::ostream& EXPECT_TRUE(bool val) {
15
        if (val) {
16
            dumpStream.str("");
17
            return dumpStream;
18
        } else {
19
            return std::cerr;
20
        }
21
    }
22
#endif
23
24
#include "blink1-lib.h"
25
26
#include "RGB.hpp"
27
#include "PlayState.hpp"
28
#include "PatternLineN.hpp"
29
#include "Blink1TestingLibrary.hpp"
30
31
using namespace blink1_lib;
32
33
std::vector<blink1_device*> fake_blink1_lib::blink1_devices;
34
std::map<long, RGB> fake_blink1_lib::ledColors;
35
std::map<long, uint16_t> fake_blink1_lib::ledFadeMillis;
36
std::map<long, PatternLineN> fake_blink1_lib::patternLines;
37
int fake_blink1_lib::cacheIndex = 0;
38
std::string fake_blink1_lib::serial;
39
bool fake_blink1_lib::isMk2;
40
uint8_t fake_blink1_lib::patternLineLEDN = 0;
41
PlayState fake_blink1_lib::playState;
42
int fake_blink1_lib::blink1Version = 0;
43
bool fake_blink1_lib::successfulOperation = false;
44
bool fake_blink1_lib::successfulInit = false;
45
bool fake_blink1_lib::degammaEnabled = false;
46
int fake_blink1_lib::vid = 0;
47
int fake_blink1_lib::pid = 0;
48
/*********************
49
 * METHODS FOR TESTS *
50
 *********************/
51
52
79
void fake_blink1_lib::CLEAR_ALL() {
53
79
    for (auto it = blink1_devices.begin(); it != blink1_devices.end(); ++it) {
54
        blink1_device* device = *it;
55
        it = blink1_devices.erase(it);
56
        delete device;
57
    }
58
59
79
    ledColors.clear();
60
79
    ledFadeMillis.clear();
61
79
    patternLines.clear();
62
63
79
    cacheIndex = 0;
64
79
    serial = "";
65
79
    isMk2 = false;
66
79
    patternLineLEDN = 0;
67
79
    playState = PlayState();
68
79
    blink1Version = 0;
69
79
    successfulOperation = false;
70
79
    successfulInit = false;
71
79
    degammaEnabled = false;
72
79
    vid = 0;
73
79
    pid = 0;
74
79
}
75
76
75
bool fake_blink1_lib::ALL_DEVICES_FREED() {
77
75
    return blink1_devices.size() == 0;
78
}
79
80
3
void fake_blink1_lib::SET_BLINK1_VERSION(int version) {
81
3
    blink1Version = version;
82
3
}
83
84
80
void fake_blink1_lib::SET_BLINK1_SUCCESSFUL_OPERATION(bool op) {
85
80
    successfulOperation = op;
86
80
}
87
88
79
void fake_blink1_lib::SET_BLINK1_SUCCESSFUL_INIT(bool init) {
89
79
    successfulInit = init;
90
79
}
91
92
3
void fake_blink1_lib::SET_BLINK1_VID(int _vid) {
93
3
    vid = _vid;
94
3
}
95
96
3
void fake_blink1_lib::SET_BLINK1_PID(int _pid) {
97
3
    pid = _pid;
98
3
}
99
100
2
void fake_blink1_lib::SET_CACHE_INDEX(int index) {
101
2
    cacheIndex = index;
102
2
}
103
104
3
void fake_blink1_lib::SET_SERIAL(std::string _serial) {
105
3
    serial = _serial;
106
3
}
107
108
9
void fake_blink1_lib::SET_IS_MK2(bool mk2) {
109
9
    isMk2 = mk2;
110
9
}
111
112
50
bool fake_blink1_lib::SUCCESS(blink1_device* dev) {
113

50
    return successfulOperation && dev != nullptr;
114
}
115
116
6
RGB fake_blink1_lib::GET_RGB(long n) {
117

6
    if (ledColors.find(n) == ledColors.end()) {
118
        ADD_FAILURE() << "LED color " << n << " has not yet been initialized.";
119
        return RGB();
120
    }
121
6
    return ledColors.at(n);
122
}
123
124
2
void fake_blink1_lib::SET_RGB(RGB rgb, long n) {
125
2
    ledColors[n] = rgb;
126
2
}
127
128
4
uint16_t fake_blink1_lib::GET_FADE_MILLIS(long n) {
129

4
    if (ledFadeMillis.find(n) == ledFadeMillis.end()) {
130
        ADD_FAILURE() << "LED fade millis " << n << " has not yet been initialized.";
131
        return 0;
132
    }
133
4
    return ledFadeMillis.at(n);
134
}
135
136
2
void fake_blink1_lib::SET_FADE_MILLIS(uint16_t fadeMillis, long n) {
137
2
    ledFadeMillis[n] = fadeMillis;
138
2
}
139
140
4
PatternLineN fake_blink1_lib::GET_PATTERN_LINE(long pos) {
141

4
    if (patternLines.find(pos) == patternLines.end()) {
142
        EXPECT_TRUE(false) << "Pattern Line " << pos << " has not yet been initialized.";
143
        return PatternLineN();
144
    }
145
4
    return patternLines.at(pos);
146
}
147
148
2
void fake_blink1_lib::SET_PATTERN_LINE(PatternLineN line, long pos) {
149
2
    patternLines[pos] = line;
150
2
}
151
152
3
PlayState fake_blink1_lib::GET_PLAY_STATE() {
153
3
    return playState;
154
}
155
156
2
void fake_blink1_lib::SET_PLAY_STATE(PlayState state) {
157
2
    playState = state;
158
2
}
159
160
/******************
161
 * MOCKED METHODS *
162
 ******************/
163
164
70
blink1_device* blink1_open() {
165
70
    if (fake_blink1_lib::successfulInit) {
166
48
        blink1_device* newDevice = new blink1_device();
167
48
        fake_blink1_lib::blink1_devices.push_back(newDevice);
168
48
        return newDevice;
169
    } else {
170
22
        return nullptr;
171
    }
172
}
173
174
blink1_device* blink1_openByPath([[maybe_unused]] const char* path) {
175
    return blink1_open();
176
}
177
178
blink1_device* blink1_openBySerial([[maybe_unused]] const char* serial) {
179
    return blink1_open();
180
}
181
182
blink1_device* blink1_openById([[maybe_unused]] uint32_t id) {
183
    return blink1_open();
184
}
185
186
48
void blink1_close_internal(blink1_device* dev) {
187
48
    auto loc = std::find(fake_blink1_lib::blink1_devices.begin(), fake_blink1_lib::blink1_devices.end(), dev);
188
48
    if (loc != fake_blink1_lib::blink1_devices.end()) {
189
48
        fake_blink1_lib::blink1_devices.erase(loc);
190
    } else {
191
        ADD_FAILURE() << "Tried to delete device that was never allocated: " << dev;
192
    }
193
194
48
    delete dev;
195
48
}
196
197
2
int blink1_getVersion(blink1_device* dev) {
198
2
    auto loc = std::find(fake_blink1_lib::blink1_devices.begin(), fake_blink1_lib::blink1_devices.end(), dev);
199
2
    if (loc != fake_blink1_lib::blink1_devices.end()) {
200
2
        return fake_blink1_lib::blink1Version;
201
    } else {
202
        //TODO docs don't specify failure case, just that device must be initialized
203
        ADD_FAILURE() << "Getting version from uninitialized device.";
204
        return -1;
205
    }
206
}
207
208
// This does LED 0 which actually sets all LEDs
209
// Does LED 0 first to make sure that it gets created in the map
210
5
int blink1_fadeToRGB(blink1_device* dev, uint16_t fadeMillis, uint8_t r, uint8_t g, uint8_t b) {
211
5
    if (fake_blink1_lib::SUCCESS(dev)) {
212
3
        fake_blink1_lib::ledFadeMillis[0] = fadeMillis;
213
3
        fake_blink1_lib::ledColors[0] = RGB(r, g, b);
214
215
6
        for (auto i = fake_blink1_lib::ledFadeMillis.begin(); i != fake_blink1_lib::ledFadeMillis.end(); ++i) {
216
3
            i->second = fadeMillis;
217
        }
218
6
        for (auto i = fake_blink1_lib::ledColors.begin(); i != fake_blink1_lib::ledColors.end(); ++i) {
219
3
            i->second = RGB(r, g, b);
220
        }
221
3
        return 0;
222
    } else {
223
2
        return -1;
224
    }
225
}
226
227
7
int blink1_fadeToRGBN(blink1_device* dev, uint16_t fadeMillis, uint8_t r, uint8_t g, uint8_t b, uint8_t n) {
228
7
    if (fake_blink1_lib::SUCCESS(dev)) {
229
4
        fake_blink1_lib::ledFadeMillis[n] = fadeMillis;
230
4
        fake_blink1_lib::ledColors[n] = RGB(r, g, b);
231
4
        return 0;
232
    } else {
233
3
        return -1;
234
    }
235
}
236
237
2
int blink1_setRGB(blink1_device* dev, uint8_t r, uint8_t g, uint8_t b) {
238
2
    if (fake_blink1_lib::SUCCESS(dev)) {
239
1
        fake_blink1_lib::ledColors[0] = RGB(r, g, b);
240
2
        for (auto i = fake_blink1_lib::ledColors.begin(); i != fake_blink1_lib::ledColors.end(); ++i) {
241
1
            i->second = RGB(r, g, b);
242
        }
243
1
        return 0;
244
    } else {
245
1
        return -1;
246
    }
247
}
248
249
4
int blink1_readRGB(blink1_device* dev, uint16_t* fadeMillis, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t ledn) {
250
4
    if (fake_blink1_lib::SUCCESS(dev)) {
251
2
        RGB ledRgb = fake_blink1_lib::GET_RGB(ledn);
252
2
        *fadeMillis = fake_blink1_lib::GET_FADE_MILLIS(ledn);
253
2
        *r = ledRgb.r;
254
2
        *g = ledRgb.g;
255
2
        *b = ledRgb.b;
256
2
        return 0;
257
    } else {
258
2
        return -1;
259
    }
260
}
261
262
4
int blink1_play(blink1_device* dev, uint8_t play, uint8_t pos) {
263
4
    if (fake_blink1_lib::SUCCESS(dev)) {
264
2
        fake_blink1_lib::playState.playing = (play == 1);
265
2
        fake_blink1_lib::playState.playPos = pos;
266
2
        fake_blink1_lib::playState.playEnd = pos;
267
2
        return 0;
268
    } else {
269
2
        return -1;
270
    }
271
}
272
273
2
int blink1_playloop(blink1_device* dev, uint8_t play, uint8_t startpos, uint8_t endpos, uint8_t count) {
274
2
    if (fake_blink1_lib::SUCCESS(dev)) {
275
1
        fake_blink1_lib::playState.playing = (play == 1);
276
1
        fake_blink1_lib::playState.playStart = startpos;
277
1
        fake_blink1_lib::playState.playEnd = endpos;
278
1
        fake_blink1_lib::playState.playCount = count;
279
1
        return 0;
280
    } else {
281
1
        return -1;
282
    }
283
}
284
285
2
int blink1_readPlayState(blink1_device* dev, uint8_t* playing, uint8_t* playstart, uint8_t* playend, uint8_t* playcount, uint8_t* playpos) {
286
2
    if (fake_blink1_lib::SUCCESS(dev)) {
287
1
        *playing = fake_blink1_lib::playState.playing ? 1 : 0;
288
1
        *playstart = fake_blink1_lib::playState.playStart;
289
1
        *playend = fake_blink1_lib::playState.playEnd;
290
1
        *playcount = fake_blink1_lib::playState.playCount;
291
1
        *playpos = fake_blink1_lib::playState.playPos;
292
1
        return 0;
293
    } else {
294
1
        return -1;
295
    }
296
}
297
298
4
int blink1_writePatternLine(blink1_device* dev, uint16_t fadeMillis, uint8_t r, uint8_t g, uint8_t b, uint8_t pos) {
299
4
    if (fake_blink1_lib::SUCCESS(dev)) {
300
2
        fake_blink1_lib::patternLines[pos] = PatternLineN(r, g, b, fake_blink1_lib::patternLineLEDN, fadeMillis);
301
2
        return 0;
302
    } else {
303
2
        return -1;
304
    }
305
}
306
307
2
int blink1_readPatternLine(blink1_device* dev, uint16_t* fadeMillis, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t pos) {
308
2
    if (fake_blink1_lib::SUCCESS(dev)) {
309
1
        PatternLineN line = fake_blink1_lib::GET_PATTERN_LINE(pos);
310
1
        *r = line.rgbn.r;
311
1
        *g = line.rgbn.g;
312
1
        *b = line.rgbn.b;
313
1
        *fadeMillis = line.fadeMillis;
314
1
        return 0;
315
    } else {
316
1
        return -1;
317
    }
318
}
319
320
2
int blink1_readPatternLineN(blink1_device* dev, uint16_t* fadeMillis, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* ledn, uint8_t pos) {
321
2
    if (fake_blink1_lib::SUCCESS(dev)) {
322
1
        PatternLineN line = fake_blink1_lib::GET_PATTERN_LINE(pos);
323
1
        *r = line.rgbn.r;
324
1
        *g = line.rgbn.g;
325
1
        *b = line.rgbn.b;
326
1
        *ledn = line.rgbn.n;
327
1
        *fadeMillis = line.fadeMillis;
328
1
        return 0;
329
    } else {
330
1
        return -1;
331
    }
332
}
333
334
2
int blink1_savePattern(blink1_device* dev) {
335
2
    if (fake_blink1_lib::SUCCESS(dev)) {
336
1
        return 0;
337
    } else {
338
1
        return -1;
339
    }
340
}
341
342
2
int blink1_setLEDN(blink1_device* dev, uint8_t ledn) {
343
2
    if (fake_blink1_lib::SUCCESS(dev)) {
344
1
        fake_blink1_lib::patternLineLEDN = ledn;
345
1
        return 0;
346
    } else {
347
1
        return -1;
348
    }
349
}
350
351
6
void blink1_enableDegamma() {
352
6
    fake_blink1_lib::degammaEnabled = true;
353
6
}
354
355
3
void blink1_disableDegamma() {
356
3
    fake_blink1_lib::degammaEnabled = false;
357
3
}
358
359
3
int blink1_vid() {
360
3
    return fake_blink1_lib::vid;
361
}
362
363
3
int blink1_pid() {
364
3
    return fake_blink1_lib::pid;
365
}
366
367
2
int blink1_getCacheIndexByDev(blink1_device* dev) {
368
2
    if (fake_blink1_lib::SUCCESS(dev)) {
369
1
        return fake_blink1_lib::cacheIndex;
370
    } else {
371
1
        return -1;
372
    }
373
}
374
375
2
int blink1_clearCacheDev(blink1_device* dev) {
376
2
    if (fake_blink1_lib::SUCCESS(dev)) {
377
1
        return fake_blink1_lib::cacheIndex;
378
    } else {
379
1
        return -1;
380
    }
381
}
382
383
2
const char* blink1_getSerialForDev(blink1_device* dev) {
384
2
    if (fake_blink1_lib::SUCCESS(dev)) {
385
1
        return fake_blink1_lib::serial.c_str();
386
    } else {
387
        // TODO ????? - Docs don't specify what happens if it's invalid
388
1
        return "";
389
    }
390
}
391
392
6
int blink1_isMk2(blink1_device* dev) {
393
6
    if (fake_blink1_lib::SUCCESS(dev)) {
394
3
        return fake_blink1_lib::isMk2;
395
    } else {
396
        // TODO ????? - Docs don't specify what happens if it's invalid
397
3
        return 0;
398
    }
399
}