Browse Source

feat: add more operator overloads

master
Dnomd343 2 years ago
parent
commit
1e219d0eef
  1. 8
      src/klotski_core/common_code/common_code.cc
  2. 7
      src/klotski_core/common_code/common_code.h
  3. 4
      src/klotski_core/raw_code/raw_code.cc
  4. 6
      src/klotski_core/raw_code/raw_code.h
  5. 10
      src/klotski_core/short_code/short_code.cc
  6. 7
      src/klotski_core/short_code/short_code.h
  7. 10
      test/codec/common_code.cc
  8. 10
      test/codec/raw_code.cc
  9. 10
      test/codec/short_code.cc

8
src/klotski_core/common_code/common_code.cc

@ -20,14 +20,14 @@ namespace std {
}
namespace klotski {
bool CommonCode::operator==(uint64_t common_code) const {
return this->code == common_code;
}
bool CommonCode::operator==(const CommonCode &common_code) const {
return this->code == common_code.code;
}
bool CommonCode::operator!=(const CommonCode &common_code) const {
return this->code != common_code.code;
}
std::ostream& operator<<(std::ostream &out, const CommonCode &self) {
char str[10];
sprintf(str, "%09lX", self.code);

7
src/klotski_core/common_code/common_code.h

@ -82,8 +82,8 @@ namespace klotski {
static bool check(uint64_t common_code);
/// Operators of CommonCode
bool operator==(uint64_t common_code) const;
bool operator==(const CommonCode &common_code) const;
bool operator!=(const CommonCode &common_code) const;
constexpr explicit operator uint64_t() const { return code; }
friend std::ostream& operator<<(std::ostream &out, const CommonCode &self);
@ -119,4 +119,9 @@ namespace klotski {
static CommonCode from_short_code(const ShortCode &short_code);
static CommonCode from_short_code(const std::string &short_code);
};
inline bool operator==(uint64_t c1, const CommonCode &c2) { return c1 == c2.unwrap(); }
inline bool operator!=(uint64_t c1, const CommonCode &c2) { return c1 != c2.unwrap(); }
inline bool operator==(const CommonCode &c1, uint64_t c2) { return c1.unwrap() == c2; }
inline bool operator!=(const CommonCode &c1, uint64_t c2) { return c1.unwrap() != c2; }
}

4
src/klotski_core/raw_code/raw_code.cc

@ -20,10 +20,6 @@ namespace std {
}
namespace klotski {
bool RawCode::operator==(uint64_t raw_code) const {
return this->code == raw_code;
}
bool RawCode::operator==(const RawCode &raw_code) const {
return this->code == raw_code.code;
}

6
src/klotski_core/raw_code/raw_code.h

@ -72,7 +72,6 @@ namespace klotski {
static bool check(uint64_t raw_code);
/// Operators of RawCode
bool operator==(uint64_t raw_code) const;
bool operator==(const RawCode &raw_code) const;
bool operator!=(const RawCode &raw_code) const;
constexpr explicit operator uint64_t() const { return code; }
@ -109,4 +108,9 @@ namespace klotski {
bool is_horizontal_mirror(RawCode &&raw_code) const; // whether horizontally symmetric to another
bool is_horizontal_mirror(const RawCode &raw_code) const;
};
inline bool operator==(uint64_t r1, const RawCode &r2) { return r1 == r2.unwrap(); }
inline bool operator!=(uint64_t r1, const RawCode &r2) { return r1 != r2.unwrap(); }
inline bool operator==(const RawCode &r1, uint64_t r2) { return r1.unwrap() == r2; }
inline bool operator!=(const RawCode &r1, uint64_t r2) { return r1.unwrap() != r2; }
}

10
src/klotski_core/short_code/short_code.cc

@ -23,15 +23,15 @@ namespace std {
}
namespace klotski {
bool ShortCode::operator==(uint32_t short_code) const {
return this->code == short_code;
}
bool ShortCode::operator==(const ShortCode &short_code) const {
return this->code == short_code.code;
}
std::ostream &operator<<(std::ostream &out, const ShortCode &self) {
bool ShortCode::operator!=(const ShortCode &short_code) const {
return this->code != short_code.code;
}
std::ostream& operator<<(std::ostream &out, const ShortCode &self) {
out << self.to_string() << "(" << self.code << ")"; // short code info
return out;
}

7
src/klotski_core/short_code/short_code.h

@ -97,8 +97,8 @@ namespace klotski {
static void speed_up(Mode mode);
/// Operators of ShortCode
bool operator==(uint32_t short_code) const;
bool operator==(const ShortCode &short_code) const;
bool operator!=(const ShortCode &short_code) const;
constexpr explicit operator uint32_t() const { return code; }
friend std::ostream& operator<<(std::ostream &out, const ShortCode &self);
@ -127,4 +127,9 @@ namespace klotski {
static ShortCode from_common_code(const CommonCode &common_code);
static ShortCode from_common_code(const std::string &common_code);
};
inline bool operator==(uint32_t s1, const ShortCode &s2) { return s1 == s2.unwrap(); }
inline bool operator!=(uint32_t s1, const ShortCode &s2) { return s1 != s2.unwrap(); }
inline bool operator==(const ShortCode &s1, uint32_t s2) { return s1.unwrap() == s2; }
inline bool operator!=(const ShortCode &s1, uint32_t s2) { return s1.unwrap() != s2; }
}

10
test/codec/common_code.cc

@ -88,9 +88,15 @@ TEST(CommonCode, operators) {
std::cout.setstate(std::ios::failbit); // hide std::cout content
std::cout << "TEST OUTPUT -> " << CommonCode(TEST_CODE) << std::endl; // ostream test
std::cout.clear();
EXPECT_EQ(CommonCode(TEST_CODE), TEST_CODE); // operator `==`
EXPECT_EQ(CommonCode(TEST_CODE), CommonCode(TEST_CODE)); // operator `==`
EXPECT_EQ((uint64_t)CommonCode(TEST_CODE), TEST_CODE); // convert as uint64_t
EXPECT_EQ(TEST_CODE, CommonCode(TEST_CODE)); // uint64_t == CommonCode
EXPECT_EQ(CommonCode(TEST_CODE), TEST_CODE); // CommonCode == uint64_t
EXPECT_EQ(CommonCode(TEST_CODE), CommonCode(TEST_CODE)); // CommonCode == CommonCode
EXPECT_NE(TEST_CODE + 1, CommonCode(TEST_CODE)); // uint64_t != CommonCode
EXPECT_NE(CommonCode(TEST_CODE), TEST_CODE + 1); // CommonCode != uint64_t
EXPECT_NE(CommonCode(TEST_CODE), CommonCode::unsafe_create(TEST_CODE + 1)); // CommonCode != CommonCode
}
TEST(CommonCode, code_convert) {

10
test/codec/raw_code.cc

@ -36,9 +36,15 @@ TEST(RawCode, operators) {
std::cout.setstate(std::ios::failbit); // hide std::cout content
std::cout << "TEST OUTPUT" << std::endl << RawCode(TEST_CODE); // ostream test
std::cout.clear();
EXPECT_EQ(RawCode(TEST_CODE), TEST_CODE); // operator `==`
EXPECT_EQ(RawCode(TEST_CODE), RawCode(TEST_CODE)); // operator `==`
EXPECT_EQ((uint64_t)RawCode(TEST_CODE), TEST_CODE); // convert as uint64_t
EXPECT_EQ(TEST_CODE, RawCode(TEST_CODE)); // uint64_t == RawCode
EXPECT_EQ(RawCode(TEST_CODE), TEST_CODE); // RawCode == uint64_t
EXPECT_EQ(RawCode(TEST_CODE), RawCode(TEST_CODE)); // RawCode == RawCode
EXPECT_NE(TEST_CODE + 1, RawCode(TEST_CODE)); // uint64_t != RawCode
EXPECT_NE(RawCode(TEST_CODE), TEST_CODE + 1); // RawCode != uint64_t
EXPECT_NE(RawCode(TEST_CODE), RawCode::unsafe_create(TEST_CODE + 1)); // RawCode != RawCode
}
TEST(RawCode, code_convert) {

10
test/codec/short_code.cc

@ -109,9 +109,15 @@ TEST(ShortCode, operators) {
std::cout.setstate(std::ios::failbit); // hide std::cout content
std::cout << "TEST OUTPUT -> " << ShortCode(TEST_CODE) << std::endl; // ostream test
std::cout.clear();
EXPECT_EQ(ShortCode(TEST_CODE), TEST_CODE); // operator `==`
EXPECT_EQ(ShortCode(TEST_CODE), ShortCode(TEST_CODE)); // operator `==`
EXPECT_EQ((uint32_t)ShortCode(TEST_CODE), TEST_CODE); // convert as uint64_t
EXPECT_EQ(TEST_CODE, ShortCode(TEST_CODE)); // uint32_t == ShortCode
EXPECT_EQ(ShortCode(TEST_CODE), TEST_CODE); // ShortCode == uint32_t
EXPECT_EQ(ShortCode(TEST_CODE), ShortCode(TEST_CODE)); // ShortCode == ShortCode
EXPECT_NE(TEST_CODE + 1, ShortCode(TEST_CODE)); // uint32_t != ShortCode
EXPECT_NE(ShortCode(TEST_CODE), TEST_CODE + 1); // ShortCode != uint32_t
EXPECT_NE(ShortCode(TEST_CODE), ShortCode::unsafe_create(TEST_CODE + 1)); // ShortCode != ShortCode
}
TEST(ShortCode, code_convert) {

Loading…
Cancel
Save