Skip to content

Commit

Permalink
fixed 日期型KQuery比较
Browse files Browse the repository at this point in the history
  • Loading branch information
fasiondog committed Sep 16, 2024
1 parent f320be9 commit ada77b9
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 4 deletions.
18 changes: 14 additions & 4 deletions hikyuu_cpp/hikyuu/KQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,24 @@ HKU_API std::ostream& operator<<(std::ostream& os, const KQuery& query) {

bool HKU_API operator!=(const KQuery& q1, const KQuery& q2) {
// cppcheck-suppress [mismatchingContainerExpression]
return q1.start() != q2.start() || q1.end() != q2.end() || q1.queryType() != q2.queryType() ||
q1.kType() != q2.kType() || q1.recoverType() != q2.recoverType();
HKU_IF_RETURN(q1.queryType() != q2.queryType(), true);
if (q1.queryType() == KQuery::DATE) {
return q1.kType() != q2.kType() || q1.recoverType() != q2.recoverType() ||
q1.startDatetime() != q2.startDatetime() || q1.endDatetime() != q2.endDatetime();
}
return q1.kType() != q2.kType() || q1.recoverType() != q2.recoverType() ||
q1.start() != q2.start() || q1.end() != q2.end();
}

bool HKU_API operator==(const KQuery& q1, const KQuery& q2) {
// cppcheck-suppress [mismatchingContainerExpression]
return q1.start() == q2.start() && q1.end() == q2.end() && q1.queryType() == q2.queryType() &&
q1.kType() == q2.kType() && q1.recoverType() == q2.recoverType();
HKU_IF_RETURN(q1.queryType() != q2.queryType(), false);
if (q1.queryType() == KQuery::DATE) {
return q1.kType() == q2.kType() && q1.recoverType() == q2.recoverType() &&
q1.startDatetime() == q2.startDatetime() && q1.endDatetime() == q2.endDatetime();
}
return q1.kType() == q2.kType() && q1.recoverType() == q2.recoverType() &&
q1.start() == q2.start() && q1.end() == q2.end();
}

} // namespace hku
73 changes: 73 additions & 0 deletions hikyuu_cpp/unit_test/hikyuu/hikyuu/test_KQuery.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2019~2023, hikyuu.org
*
* History:
* 1. 20240916 added by fasiondog
*/

#include "../test_config.h"
#include <hikyuu/KQuery.h>

using namespace hku;

/**
* @defgroup test_hikyuu_KQuery test_hikyuu_KQuery
* @ingroup test_hikyuu_base_suite
* @{
*/

/** @par 检测点 */
TEST_CASE("test_KQuery") {
KQuery q1 = KQueryByIndex(-1);
CHECK_EQ(q1.start(), -1);
CHECK_UNARY(q1.end() == Null<int64_t>());
CHECK_UNARY(q1.startDatetime() == Null<Datetime>());
CHECK_UNARY(q1.endDatetime() == Null<Datetime>());

q1 = KQueryByDate(Datetime(20010101), Datetime(20010110));
CHECK_UNARY(q1.start() == Null<int64_t>());
CHECK_UNARY(q1.end() == Null<int64_t>());
CHECK_EQ(q1.startDatetime(), Datetime(20010101));
CHECK_EQ(q1.endDatetime(), Datetime(20010110));
}

/** @par 检测点 */
TEST_CASE("test_KQuery_equal") {
KQuery q1 = KQueryByIndex(-1);
KQuery q2 = KQueryByIndex(-1);
CHECK_EQ(q1, q2);

q1 = KQueryByIndex(-1);
q2 = KQueryByIndex(2);
CHECK_NE(q1, q2);

q1 = KQueryByIndex(2);
q2 = KQueryByIndex(2, 3);
CHECK_NE(q1, q2);

q1 = KQueryByIndex(2);
q2 = KQueryByIndex(2);
CHECK_EQ(q1, q2);

q1 = KQueryByIndex(2, 10);
q2 = KQueryByIndex(2, 10);
CHECK_EQ(q1, q2);

q1 = KQueryByDate(Datetime(20010101));
q2 = KQueryByIndex(2, 10);
CHECK_NE(q1, q2);

q1 = KQueryByDate(Datetime(20010101));
q2 = KQueryByDate(Datetime(20010101));
CHECK_EQ(q1, q2);

q1 = KQueryByDate(Datetime(20010101));
q2 = KQueryByDate(Datetime(20010101), Datetime(20010102));
CHECK_NE(q1, q2);

q1 = KQueryByDate(Datetime(20010101), Datetime(20010110));
q2 = KQueryByDate(Datetime(20010101), Datetime(20010110));
CHECK_EQ(q1, q2);
}

/** @} */

0 comments on commit ada77b9

Please sign in to comment.