1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
#include <gtest/gtest.h>
#include <QtDebug>
#include "track/playcounter.h"
#include "util/math.h"
class PlayCounterTest : public testing::Test {
protected:
void updatePlayedAndVerify(PlayCounter* pPlayCounter, bool bPlayed) {
bool isPlayedBefore = pPlayCounter->isPlayed();
int timesPlayedBefore = pPlayCounter->getTimesPlayed();
pPlayCounter->setPlayedAndUpdateTimesPlayed(bPlayed);
bool isPlayedAfter = pPlayCounter->isPlayed();
int timesPlayedAfter = pPlayCounter->getTimesPlayed();
if (bPlayed) {
EXPECT_TRUE(isPlayedAfter);
EXPECT_EQ(timesPlayedBefore + 1, timesPlayedAfter);
} else {
EXPECT_FALSE(isPlayedAfter);
if (isPlayedBefore) {
EXPECT_EQ(math_max(0, timesPlayedBefore - 1), timesPlayedAfter);
} else {
EXPECT_EQ(timesPlayedBefore, timesPlayedAfter);
}
}
}
void resetAndVerify(PlayCounter* pPlayCounter) {
*pPlayCounter = PlayCounter();
bool isPlayedAfter = pPlayCounter->isPlayed();
int timesPlayedAfter = pPlayCounter->getTimesPlayed();
EXPECT_FALSE(isPlayedAfter);
EXPECT_EQ(0, timesPlayedAfter);
}
void testCycle(PlayCounter* pPlayCounter) {
updatePlayedAndVerify(pPlayCounter, false);
updatePlayedAndVerify(pPlayCounter, true);
updatePlayedAndVerify(pPlayCounter, false);
updatePlayedAndVerify(pPlayCounter, true);
updatePlayedAndVerify(pPlayCounter, true);
resetAndVerify(pPlayCounter);
updatePlayedAndVerify(pPlayCounter, true);
updatePlayedAndVerify(pPlayCounter, false);
updatePlayedAndVerify(pPlayCounter, false);
updatePlayedAndVerify(pPlayCounter, true);
updatePlayedAndVerify(pPlayCounter, true);
updatePlayedAndVerify(pPlayCounter, false);
EXPECT_EQ(PlayCounter(1), *pPlayCounter);
resetAndVerify(pPlayCounter);
EXPECT_EQ(PlayCounter(), *pPlayCounter);
}
};
TEST_F(PlayCounterTest, DefaultCounstructor) {
PlayCounter playCounter;
EXPECT_FALSE(playCounter.isPlayed());
EXPECT_EQ(0, playCounter.getTimesPlayed());
testCycle(&playCounter);
}
TEST_F(PlayCounterTest, InitializingCounstructor) {
PlayCounter playCounter(5);
EXPECT_FALSE(playCounter.isPlayed());
EXPECT_EQ(5, playCounter.getTimesPlayed());
testCycle(&playCounter);
}
|