Skip to content

Commit d488b26

Browse files
committed
Add/fixes unit test
1 parent ae183c2 commit d488b26

13 files changed

+3258
-47
lines changed

test/base64_test.cpp

Lines changed: 536 additions & 4 deletions
Large diffs are not rendered by default.

test/bit_segment_test.cpp

Lines changed: 400 additions & 0 deletions
Large diffs are not rendered by default.

test/button_status_test.cpp

Lines changed: 451 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*/
6+
/*
7+
UnitTest for compatibility_feature (Arduino API compatibility)
8+
*/
9+
#include <gtest/gtest.h>
10+
#include <M5Utility.hpp>
11+
12+
using m5::utility::delay;
13+
using m5::utility::delayMicroseconds;
14+
using m5::utility::micros;
15+
using m5::utility::millis;
16+
17+
TEST(CompatibilityFeature, MillisIncreasing)
18+
{
19+
// millis() should return increasing values
20+
unsigned long t1 = millis();
21+
delay(10);
22+
unsigned long t2 = millis();
23+
24+
EXPECT_GT(t2, t1) << "millis() should increase over time";
25+
}
26+
27+
TEST(CompatibilityFeature, MicrosIncreasing)
28+
{
29+
// micros() should return increasing values
30+
unsigned long t1 = micros();
31+
delayMicroseconds(100);
32+
unsigned long t2 = micros();
33+
34+
EXPECT_GT(t2, t1) << "micros() should increase over time";
35+
}
36+
37+
TEST(CompatibilityFeature, MicrosGreaterThanMillis)
38+
{
39+
// micros() should be approximately millis() * 1000
40+
unsigned long ms = millis();
41+
unsigned long us = micros();
42+
43+
// Allow some tolerance for execution time between calls
44+
// micros should be at least millis * 1000 (minus small tolerance)
45+
EXPECT_GE(us + 1000, ms * 1000) << "micros() should be >= millis() * 1000";
46+
}
47+
48+
TEST(CompatibilityFeature, DelayApproximateTime)
49+
{
50+
// delay() should pause for approximately the specified time
51+
const unsigned long delay_ms = 50;
52+
const unsigned long tolerance_ms = 20; // Allow 20ms tolerance
53+
54+
unsigned long start = millis();
55+
delay(delay_ms);
56+
unsigned long elapsed = millis() - start;
57+
58+
EXPECT_GE(elapsed, delay_ms - tolerance_ms)
59+
<< "delay() should pause for at least " << (delay_ms - tolerance_ms) << "ms";
60+
EXPECT_LE(elapsed, delay_ms + tolerance_ms)
61+
<< "delay() should not pause for more than " << (delay_ms + tolerance_ms) << "ms";
62+
}
63+
64+
TEST(CompatibilityFeature, DelayMicrosecondsApproximateTime)
65+
{
66+
// delayMicroseconds() should pause for approximately the specified time
67+
const unsigned int delay_us = 5000; // 5ms in microseconds
68+
const unsigned long tolerance_us = 3000; // Allow 3ms tolerance
69+
70+
unsigned long start = micros();
71+
delayMicroseconds(delay_us);
72+
unsigned long elapsed = micros() - start;
73+
74+
EXPECT_GE(elapsed, delay_us - tolerance_us)
75+
<< "delayMicroseconds() should pause for at least " << (delay_us - tolerance_us) << "us";
76+
EXPECT_LE(elapsed, delay_us + tolerance_us)
77+
<< "delayMicroseconds() should not pause for more than " << (delay_us + tolerance_us) << "us";
78+
}
79+
80+
TEST(CompatibilityFeature, DelayZero)
81+
{
82+
// delay(0) should return quickly
83+
unsigned long start = millis();
84+
delay(0);
85+
unsigned long elapsed = millis() - start;
86+
87+
EXPECT_LE(elapsed, 5U) << "delay(0) should return quickly";
88+
}
89+
90+
TEST(CompatibilityFeature, DelayMicrosecondsZero)
91+
{
92+
// delayMicroseconds(0) should return quickly
93+
unsigned long start = micros();
94+
delayMicroseconds(0);
95+
unsigned long elapsed = micros() - start;
96+
97+
EXPECT_LE(elapsed, 1000U) << "delayMicroseconds(0) should return quickly";
98+
}
99+
100+
TEST(CompatibilityFeature, MultipleDelays)
101+
{
102+
// Multiple delays should accumulate
103+
const unsigned long delay_ms = 20;
104+
const int count = 3;
105+
const unsigned long tolerance_ms = 30;
106+
107+
unsigned long start = millis();
108+
for (int i = 0; i < count; ++i) {
109+
delay(delay_ms);
110+
}
111+
unsigned long elapsed = millis() - start;
112+
113+
unsigned long expected = delay_ms * count;
114+
EXPECT_GE(elapsed, expected - tolerance_ms);
115+
EXPECT_LE(elapsed, expected + tolerance_ms);
116+
}
117+
118+
TEST(CompatibilityFeature, ConsistencyBetweenMillisAndMicros)
119+
{
120+
// The difference between consecutive millis/micros calls should be consistent
121+
unsigned long ms1 = millis();
122+
unsigned long us1 = micros();
123+
124+
delay(100);
125+
126+
unsigned long ms2 = millis();
127+
unsigned long us2 = micros();
128+
129+
unsigned long ms_diff = ms2 - ms1;
130+
unsigned long us_diff = us2 - us1;
131+
132+
// us_diff should be approximately ms_diff * 1000 (with tolerance)
133+
unsigned long expected_us = ms_diff * 1000;
134+
unsigned long tolerance = 10000; // 10ms tolerance
135+
136+
EXPECT_GE(us_diff + tolerance, expected_us);
137+
EXPECT_LE(us_diff, expected_us + tolerance);
138+
}

test/container_test.cpp

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,247 @@ void cb_iterator_test()
462462
}
463463
}
464464

465+
void cb_copy_move_test()
466+
{
467+
SCOPED_TRACE("Copy/Move");
468+
469+
// Copy constructor
470+
{
471+
FixedCircularBuffer<int, 4> original = {1, 2, 3};
472+
FixedCircularBuffer<int, 4> copied(original);
473+
474+
EXPECT_EQ(copied.size(), 3U);
475+
EXPECT_EQ(copied[0], 1);
476+
EXPECT_EQ(copied[1], 2);
477+
EXPECT_EQ(copied[2], 3);
478+
479+
// Verify original unchanged
480+
EXPECT_EQ(original.size(), 3U);
481+
EXPECT_EQ(original[0], 1);
482+
}
483+
484+
// Move constructor
485+
{
486+
FixedCircularBuffer<int, 4> original = {4, 5, 6};
487+
FixedCircularBuffer<int, 4> moved(std::move(original));
488+
489+
EXPECT_EQ(moved.size(), 3U);
490+
EXPECT_EQ(moved[0], 4);
491+
EXPECT_EQ(moved[1], 5);
492+
EXPECT_EQ(moved[2], 6);
493+
}
494+
495+
// Copy assignment
496+
{
497+
FixedCircularBuffer<int, 4> original = {7, 8};
498+
FixedCircularBuffer<int, 4> assigned;
499+
assigned = original;
500+
501+
EXPECT_EQ(assigned.size(), 2U);
502+
EXPECT_EQ(assigned[0], 7);
503+
EXPECT_EQ(assigned[1], 8);
504+
}
505+
506+
// Move assignment
507+
{
508+
FixedCircularBuffer<int, 4> original = {9, 10};
509+
FixedCircularBuffer<int, 4> assigned;
510+
assigned = std::move(original);
511+
512+
EXPECT_EQ(assigned.size(), 2U);
513+
EXPECT_EQ(assigned[0], 9);
514+
EXPECT_EQ(assigned[1], 10);
515+
}
516+
}
517+
518+
void cb_swap_test()
519+
{
520+
SCOPED_TRACE("Swap");
521+
522+
// Member swap
523+
{
524+
FixedCircularBuffer<int, 4> a = {1, 2, 3};
525+
FixedCircularBuffer<int, 4> b = {4, 5};
526+
527+
a.swap(b);
528+
529+
EXPECT_EQ(a.size(), 2U);
530+
EXPECT_EQ(a[0], 4);
531+
EXPECT_EQ(a[1], 5);
532+
533+
EXPECT_EQ(b.size(), 3U);
534+
EXPECT_EQ(b[0], 1);
535+
EXPECT_EQ(b[1], 2);
536+
EXPECT_EQ(b[2], 3);
537+
}
538+
539+
// std::swap specialization
540+
{
541+
FixedCircularBuffer<int, 4> a = {10, 20};
542+
FixedCircularBuffer<int, 4> b = {30, 40, 50};
543+
544+
std::swap(a, b);
545+
546+
EXPECT_EQ(a.size(), 3U);
547+
EXPECT_EQ(a[0], 30);
548+
EXPECT_EQ(a[1], 40);
549+
EXPECT_EQ(a[2], 50);
550+
551+
EXPECT_EQ(b.size(), 2U);
552+
EXPECT_EQ(b[0], 10);
553+
EXPECT_EQ(b[1], 20);
554+
}
555+
556+
// Swap with empty
557+
{
558+
FixedCircularBuffer<int, 4> a = {1, 2, 3, 4};
559+
FixedCircularBuffer<int, 4> b;
560+
561+
a.swap(b);
562+
563+
EXPECT_TRUE(a.empty());
564+
EXPECT_TRUE(b.full());
565+
EXPECT_EQ(b.size(), 4U);
566+
}
567+
568+
// Self swap
569+
{
570+
FixedCircularBuffer<int, 4> a = {1, 2, 3};
571+
a.swap(a);
572+
573+
EXPECT_EQ(a.size(), 3U);
574+
EXPECT_EQ(a[0], 1);
575+
EXPECT_EQ(a[1], 2);
576+
EXPECT_EQ(a[2], 3);
577+
}
578+
}
579+
580+
void cb_assign_test()
581+
{
582+
SCOPED_TRACE("Assign");
583+
584+
// assign with initializer_list
585+
{
586+
FixedCircularBuffer<int, 4> rb = {1, 2, 3, 4};
587+
rb.assign({10, 20});
588+
589+
EXPECT_EQ(rb.size(), 2U);
590+
EXPECT_EQ(rb[0], 10);
591+
EXPECT_EQ(rb[1], 20);
592+
}
593+
594+
// assign with count and value (use size_t to avoid template overload)
595+
{
596+
FixedCircularBuffer<int, 4> rb;
597+
rb.assign(size_t(3), 42);
598+
599+
EXPECT_EQ(rb.size(), 3U);
600+
EXPECT_EQ(rb[0], 42);
601+
EXPECT_EQ(rb[1], 42);
602+
EXPECT_EQ(rb[2], 42);
603+
}
604+
605+
// assign with iterators
606+
{
607+
std::vector<int> src = {100, 200, 300};
608+
FixedCircularBuffer<int, 4> rb;
609+
rb.assign(src.begin(), src.end());
610+
611+
EXPECT_EQ(rb.size(), 3U);
612+
EXPECT_EQ(rb[0], 100);
613+
EXPECT_EQ(rb[1], 200);
614+
EXPECT_EQ(rb[2], 300);
615+
}
616+
}
617+
618+
void cb_dynamic_buffer_test()
619+
{
620+
SCOPED_TRACE("DynamicBuffer");
621+
622+
// CircularBuffer (dynamic size)
623+
{
624+
CircularBuffer<int> rb(4);
625+
626+
EXPECT_TRUE(rb.empty());
627+
EXPECT_EQ(rb.capacity(), 4U);
628+
629+
rb.push_back(1);
630+
rb.push_back(2);
631+
rb.push_back(3);
632+
633+
EXPECT_EQ(rb.size(), 3U);
634+
EXPECT_EQ(rb[0], 1);
635+
EXPECT_EQ(rb[1], 2);
636+
EXPECT_EQ(rb[2], 3);
637+
}
638+
639+
// CircularBuffer with assign (full fill)
640+
{
641+
CircularBuffer<int> rb(4);
642+
rb.assign(size_t(4), 99);
643+
644+
EXPECT_TRUE(rb.full());
645+
EXPECT_EQ(rb.size(), 4U);
646+
EXPECT_EQ(rb[0], 99);
647+
EXPECT_EQ(rb[1], 99);
648+
EXPECT_EQ(rb[2], 99);
649+
EXPECT_EQ(rb[3], 99);
650+
}
651+
652+
// CircularBuffer with assign (partial fill)
653+
{
654+
CircularBuffer<int> rb(4);
655+
rb.assign(size_t(3), 99);
656+
657+
EXPECT_EQ(rb.size(), 3U);
658+
EXPECT_EQ(rb[0], 99);
659+
EXPECT_EQ(rb[1], 99);
660+
EXPECT_EQ(rb[2], 99);
661+
}
662+
663+
// CircularBuffer with iterator constructor
664+
{
665+
std::vector<int> src = {1, 2, 3, 4, 5};
666+
CircularBuffer<int> rb(3, src.begin(), src.end());
667+
668+
EXPECT_TRUE(rb.full());
669+
EXPECT_EQ(rb.size(), 3U);
670+
EXPECT_EQ(rb[0], 3); // Last 3 elements
671+
EXPECT_EQ(rb[1], 4);
672+
EXPECT_EQ(rb[2], 5);
673+
}
674+
675+
// CircularBuffer with initializer_list constructor
676+
{
677+
CircularBuffer<int> rb(4, {10, 20, 30});
678+
679+
EXPECT_EQ(rb.size(), 3U);
680+
EXPECT_EQ(rb[0], 10);
681+
EXPECT_EQ(rb[1], 20);
682+
EXPECT_EQ(rb[2], 30);
683+
}
684+
685+
// CircularBuffer swap
686+
{
687+
CircularBuffer<int> a(4);
688+
CircularBuffer<int> b(6);
689+
690+
a.push_back(1);
691+
a.push_back(2);
692+
b.push_back(10);
693+
694+
a.swap(b);
695+
696+
EXPECT_EQ(a.capacity(), 6U);
697+
EXPECT_EQ(a.size(), 1U);
698+
EXPECT_EQ(a[0], 10);
699+
700+
EXPECT_EQ(b.capacity(), 4U);
701+
EXPECT_EQ(b.size(), 2U);
702+
EXPECT_EQ(b[0], 1);
703+
}
704+
}
705+
465706
} // namespace
466707

467708
TEST(Utility, CircularBuffer)
@@ -470,4 +711,8 @@ TEST(Utility, CircularBuffer)
470711
cb_constructor_test();
471712
cb_read();
472713
cb_iterator_test();
714+
cb_copy_move_test();
715+
cb_swap_test();
716+
cb_assign_test();
717+
cb_dynamic_buffer_test();
473718
}

0 commit comments

Comments
 (0)