Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
aiekick committed Oct 29, 2023
1 parent 91d46d7 commit af8f71a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
36 changes: 35 additions & 1 deletion include/ctools/cTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,39 @@ using namespace cocos2d;
namespace ct // cTools
{

/////////////////////////////////////////////
/////////////////////////////////////////////
/////////////////////////////////////////////

// is a user datas of type void* is used
// we cant do a dynamic_cast, so for test if we have the good class
// we can use a magic number, because this member will not cause compiling issue
// and if this is the good class, the magic_number will point to a random memory zone
// so will not have the good value

inline int64_t EncodeId(const std::string& vArr) {
if (vArr.empty() || vArr.size() != 8U) {
return 0;
}
return vArr[0] | //
(vArr[1] << 8) | //
(vArr[2] << 16) | //
(vArr[3] << 24) | //
((int64_t)(vArr[4]) << 32) | //
((int64_t)(vArr[5]) << 40) | //
((int64_t)(vArr[6]) << 48) | //
((int64_t)(vArr[7]) << 56);
}

inline bool IsIdEqualTo(const int64_t& vId, char vArr[8]) {
return (EncodeId(vArr) == vId);
}

/////////////////////////////////////////////
/////////////////////////////////////////////
/////////////////////////////////////////////
/////////////////////////////////////////////

template <typename T>
class SearchableVector {
private:
Expand Down Expand Up @@ -198,7 +231,8 @@ class SearchableVector {
}
return false;
}
bool exist(const T& vKey) const { return (m_Dico.find(vKey) != m_Dico.end()); }
bool exist(const T& vKey) const {
return (m_Dico.find(vKey) != m_Dico.end()); }
};

CTOOLS_API std::string toStr(const char* fmt, ...);
Expand Down
4 changes: 4 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,7 @@ AddTest("cTools.float.Plane.construtor.point_and_normal")
AddTest("cTools.float.Plane.construtor.3_points")
AddTest("cTools.float.Plane.get_plane_intersection")
AddTest("cTools.float.Plane.is_on_plane")

### TESTS for cTools.EncodeID

AddTest("cTools.EncodeID")
32 changes: 31 additions & 1 deletion tests/TestCode/cTools/Test_cTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2792,6 +2792,30 @@ int Test_cTools_float_double_Plane_run_test(const std::string& vTestCode)

return 1; // error
}

///////////////////////////////////////////////////////////
//// ENCODE ID ////////////////////////////////////////////
///////////////////////////////////////////////////////////

int Test_cTools_Encode_ID() {
if (ct::EncodeId("lgfkhklfgjhlgkfkjfghlkfgh") != 0) { // more than 8
return 1; // error
}
if (ct::EncodeId("") != 0) { // empty
return 1; // error
}
if (ct::EncodeId("IProject") != 5282848185757557618) {
return 1; // error
}
if (!ct::IsIdEqualTo(5282848185757557618, "IProject")) {
return 1; // error
}
if (ct::IsIdEqualTo(5282848185757557618, "TOTO")) {
return 1; // error
}
return 0; // no error
}

///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
Expand Down Expand Up @@ -2876,7 +2900,13 @@ int Test_cTools_run_test(const std::string& vTestCode)
else if (vTestCode.find("cTools.float.Plane.") != std::string::npos)
{
return Test_cTools_float_double_Plane_run_test<float>(vTestCode);
}
}

// Encode ID

else if (vTestCode.find("cTools.EncodeID") != std::string::npos) {
return Test_cTools_Encode_ID();
}

return 1; // error
}

0 comments on commit af8f71a

Please sign in to comment.