Skip to content

Commit db28ab7

Browse files
committed
RGB565: unit tests and default constructor zeroes memory
1 parent 9980db6 commit db28ab7

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

tests/rgb565_tests.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "doctest.h"
2+
#include "util/colors_rgb565.hh"
3+
4+
TEST_CASE("Basic usage") {
5+
6+
RGB565 c1;
7+
RGB565 c2{};
8+
RGB565 c3{(uint8_t)0x80, 0x40, 0x20};
9+
RGB565 c4{0.5f, 0.25f, 0.125f};
10+
RGB565 c5{0x804020};
11+
12+
CHECK(c1.raw() == 0);
13+
CHECK(c2.raw() == 0);
14+
CHECK(c3.raw() == 0x8204);
15+
CHECK(c4.raw() == 0x8204);
16+
CHECK(c5.raw() == 0x8204);
17+
18+
CHECK(c5.red() == 0x80);
19+
CHECK(c5.green() == 0x40);
20+
CHECK(c5.blue() == 0x20);
21+
22+
c1 = 0x2334;
23+
CHECK(c1.raw() == 0x2334);
24+
CHECK((uint16_t)c1 == 0x2334);
25+
}

util/colors_rgb565.hh

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55

66
struct RGB565 {
77
// msb> rrrrr gggggg bbbbb <lsb
8-
uint16_t b : 5;
9-
uint16_t g : 6;
10-
uint16_t r : 5;
8+
uint16_t b : 5 {};
9+
uint16_t g : 6 {};
10+
uint16_t r : 5 {};
1111

12-
constexpr RGB565() {
12+
constexpr RGB565()
13+
: b{0}
14+
, g{0}
15+
, r{0} {
1316
}
1417

1518
constexpr RGB565(uint8_t red, uint8_t green, uint8_t blue)

0 commit comments

Comments
 (0)