Fix General/SegmentSupportHeightCalls

This commit is contained in:
Marijn van der Werf 2016-10-17 23:12:54 +02:00
parent 4f10555002
commit a44745e2fa
6 changed files with 50 additions and 48 deletions

View File

@ -27,7 +27,7 @@ bool GeneralSupportHeightCall::CallsMatch(SupportCall tileSupportCalls[4]) {
return true;
}
SupportCall *GeneralSupportHeightCall::FindMostCommonSupportCall(SupportCall calls[4]) {
bool GeneralSupportHeightCall::FindMostCommonSupportCall(SupportCall calls[4], SupportCall *out) {
std::map<SupportCall, int> map;
for (int i = 0; i < 4; ++i) {
@ -39,30 +39,33 @@ SupportCall *GeneralSupportHeightCall::FindMostCommonSupportCall(SupportCall cal
}
if (map.size() == 1) {
return &calls[0];
(*out) = calls[0];
return true;
}
if (map.size() == 2) {
for (auto &&item : map) {
if (item.second == 3) {
return (SupportCall *)&item.first;
(*out) = item.first;
return true;
}
}
return nullptr;
return false;
}
if (map.size() == 3) {
for (auto &&item : map) {
if (item.second == 2) {
return (SupportCall *)&item.first;
if (item.second == 2) {
(*out) = item.first;
return true;
}
}
return nullptr;
return false;
}
return nullptr;
return false;
}
bool GeneralSupportHeightCall::AssertEquals(const SupportCall *lhs, const SupportCall *rhs) {

View File

@ -45,7 +45,7 @@ struct SupportCall {
namespace GeneralSupportHeightCall {
bool CallsMatch(SupportCall tileSupportCalls[4]);
SupportCall *FindMostCommonSupportCall(SupportCall calls[4]);
bool FindMostCommonSupportCall(SupportCall calls[4], SupportCall *out);
bool AssertEquals(const SupportCall *lhs, const SupportCall *rhs);
};

View File

@ -114,40 +114,39 @@ bool SegmentSupportHeightCall::CallsEqual(std::vector<SegmentSupportCall> lhs, s
return true;
}
std::vector<SegmentSupportCall>* SegmentSupportHeightCall::FindMostCommonSupportCall(std::vector<SegmentSupportCall> *calls) {
bool SegmentSupportHeightCall::FindMostCommonSupportCall(std::vector<SegmentSupportCall> calls[4], std::vector<SegmentSupportCall> *out) {
std::map<std::vector<SegmentSupportCall>, int> map;
for (int i = 0; i < 4; ++i) {
if (map.count(calls[i]) == 0) {
map[calls[i]] = 1;
} else {
map[calls[i]] += 1;
}
map[calls[i]] += 1;
}
if (map.size() == 1) {
return &calls[0];
(*out) = calls[0];
return true;
}
if (map.size() == 2) {
for (auto &&item : map) {
if (item.second == 3) {
return (std::vector<SegmentSupportCall> *)&item.first;
(*out) = item.first;
return true;
}
}
return nullptr;
return false;
}
if (map.size() == 3) {
for (auto &&item : map) {
if (item.second == 2) {
return (std::vector<SegmentSupportCall> *)&item.first;
(*out) = item.first;
return true;
}
}
return nullptr;
return false;
}
return nullptr;
return false;
}

View File

@ -48,5 +48,5 @@ public:
static std::vector<SegmentSupportCall> getSegmentCalls(support_height supports[9], uint8 rotation);
static bool CallsMatch(std::vector<SegmentSupportCall> tileSegmentSupportCalls[4]);
static bool CallsEqual(std::vector<SegmentSupportCall> lhs, std::vector<SegmentSupportCall> rhs);
static std::vector<SegmentSupportCall> * FindMostCommonSupportCall(std::vector<SegmentSupportCall> calls[4]);
static bool FindMostCommonSupportCall(std::vector<SegmentSupportCall> calls[4], std::vector<SegmentSupportCall> * out);
};

View File

@ -433,11 +433,8 @@ static uint8 TestTrackElementSegmentSupportHeight(uint8 rideType, uint8 trackTyp
std::vector<SegmentSupportCall> referenceCalls = tileSegmentSupportCalls[0];
if (!SegmentSupportHeightCall::CallsMatch(tileSegmentSupportCalls)) {
std::vector<SegmentSupportCall> *found = SegmentSupportHeightCall::FindMostCommonSupportCall(
tileSegmentSupportCalls);
if (found != nullptr) {
referenceCalls = *found;
} else {
bool success = SegmentSupportHeightCall::FindMostCommonSupportCall(tileSegmentSupportCalls, &referenceCalls);
if (!success) {
*error += String::Format("Original segment calls didn't match. %s\n", state.c_str());
for (int direction = 0; direction < 4; direction++) {
*error += String::Format("# %d\n", direction);
@ -516,10 +513,10 @@ static uint8 TestTrackElementGeneralSupportHeight(uint8 rideType, uint8 trackTyp
}
}
SupportCall referenceGeneralSupportCall = tileGeneralSupportCalls[0];
SupportCall referenceCall = tileGeneralSupportCalls[0];
if (!GeneralSupportHeightCall::CallsMatch(tileGeneralSupportCalls)) {
SupportCall *found = GeneralSupportHeightCall::FindMostCommonSupportCall(tileGeneralSupportCalls);
if (found == nullptr) {
bool success = GeneralSupportHeightCall::FindMostCommonSupportCall(tileGeneralSupportCalls, &referenceCall);
if (!success) {
*error += String::Format("Original support calls didn't match. %s\n", state.c_str());
for (int i = 0; i < 4; ++i) {
*error += String::Format("[%d, 0x%02X] ", tileGeneralSupportCalls[i].height, tileGeneralSupportCalls[i].slope);
@ -527,7 +524,6 @@ static uint8 TestTrackElementGeneralSupportHeight(uint8 rideType, uint8 trackTyp
*error += "\n";
return TEST_FAILED;
}
referenceGeneralSupportCall = *found;
}
for (int direction = 0; direction < 4; direction++) {
@ -540,11 +536,11 @@ static uint8 TestTrackElementGeneralSupportHeight(uint8 rideType, uint8 trackTyp
}
if (referenceGeneralSupportCall.height != -1) {
if (gSupport.height != referenceGeneralSupportCall.height) {
if (referenceCall.height != -1) {
if (gSupport.height != referenceCall.height) {
*error += String::Format(
"General support heights didn't match. (expected height + %d, actual: height + %d) [direction:%d] %s\n",
referenceGeneralSupportCall.height - height,
referenceCall.height - height,
gSupport.height - height,
direction,
state.c_str()
@ -552,11 +548,11 @@ static uint8 TestTrackElementGeneralSupportHeight(uint8 rideType, uint8 trackTyp
return TEST_FAILED;
}
}
if (referenceGeneralSupportCall.slope != -1) {
if (gSupport.slope != referenceGeneralSupportCall.slope) {
if (referenceCall.slope != -1) {
if (gSupport.slope != referenceCall.slope) {
*error += String::Format(
"General support slopes didn't match. (expected 0x%02X, actual: 0x%02X) [direction:%d] %s\n",
referenceGeneralSupportCall.slope,
referenceCall.slope,
gSupport.slope,
direction,
state.c_str()

View File

@ -366,27 +366,31 @@ static void TestGeneralSupportHeightCall() {
SupportCall callC = {48, 0x20};
SupportCall callD = {48, 0x1F};
SupportCall *result;
SupportCall out = {0,0};
bool success;
SupportCall groupA[4] = {callA, callA, callA, callA};
result = GeneralSupportHeightCall::FindMostCommonSupportCall(groupA);
assert(GeneralSupportHeightCall::AssertEquals(result, &callA));
success = GeneralSupportHeightCall::FindMostCommonSupportCall(groupA, &out);
assert(success);
assert(out == callA);
SupportCall groupB[4] = {callB, callA, callA, callA};
result = GeneralSupportHeightCall::FindMostCommonSupportCall(groupB);
assert(GeneralSupportHeightCall::AssertEquals(result, &callA));
success = GeneralSupportHeightCall::FindMostCommonSupportCall(groupB, &out);
assert(success);
assert(out == callA);
SupportCall groupC[4] = {callB, callA, callB, callA};
result = GeneralSupportHeightCall::FindMostCommonSupportCall(groupC);
assert(GeneralSupportHeightCall::AssertEquals(result, nullptr));
success = GeneralSupportHeightCall::FindMostCommonSupportCall(groupC, &out);
assert(!success);
SupportCall groupD[4] = {callB, callC, callB, callA};
result = GeneralSupportHeightCall::FindMostCommonSupportCall(groupD);
assert(GeneralSupportHeightCall::AssertEquals(result, &callB));
success = GeneralSupportHeightCall::FindMostCommonSupportCall(groupD, &out);
assert(success);
assert(out == callB);
SupportCall groupE[4] = {callD, callC, callB, callA};
result = GeneralSupportHeightCall::FindMostCommonSupportCall(groupE);
assert(GeneralSupportHeightCall::AssertEquals(result, nullptr));
success = GeneralSupportHeightCall::FindMostCommonSupportCall(groupE, &out);
assert(!success);
}
int main(int argc, char *argv[]) {