Skip to content

Commit 0dce5bc

Browse files
author
Christian Hergert
committed
matcher: fix some inconsistencies in matcher logic.
1 parent 47252e9 commit 0dce5bc

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

src/mongoc/mongoc-matcher-op.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -372,20 +372,15 @@ _mongoc_matcher_op_not_match (mongoc_matcher_op_not_t *not, /* IN */
372372
}
373373

374374

375-
/*
376-
* NOTE: The compare operators are opposite since the left and right are
377-
* somewhat inverted being that the spec is on the left and the value on the
378-
* right.
379-
*/
380375
#define _TYPE_CODE(l, r) ((((int)(l)) << 8) | ((int)(r)))
381376
#define _NATIVE_COMPARE(op, t1, t2) \
382-
(bson_iter##t1(&compare->iter) op bson_iter##t2(iter))
377+
(bson_iter##t2(iter) op bson_iter##t1(&compare->iter))
383378
#define _EQ_COMPARE(t1, t2) _NATIVE_COMPARE(==, t1, t2)
384379
#define _NE_COMPARE(t1, t2) _NATIVE_COMPARE(!=, t1, t2)
385-
#define _GT_COMPARE(t1, t2) _NATIVE_COMPARE(<=, t1, t2)
386-
#define _GTE_COMPARE(t1, t2) _NATIVE_COMPARE(<, t1, t2)
387-
#define _LT_COMPARE(t1, t2) _NATIVE_COMPARE(>=, t1, t2)
388-
#define _LTE_COMPARE(t1, t2) _NATIVE_COMPARE(>, t1, t2)
380+
#define _GT_COMPARE(t1, t2) _NATIVE_COMPARE(>, t1, t2)
381+
#define _GTE_COMPARE(t1, t2) _NATIVE_COMPARE(>=, t1, t2)
382+
#define _LT_COMPARE(t1, t2) _NATIVE_COMPARE(<, t1, t2)
383+
#define _LTE_COMPARE(t1, t2) _NATIVE_COMPARE(<=, t1, t2)
389384

390385

391386
/*

tests/test-mongoc-matcher.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,44 @@ test_mongoc_matcher_basic (void)
7474
}
7575

7676

77+
typedef struct compare_check
78+
{
79+
const char *op;
80+
int32_t doc;
81+
int32_t query;
82+
bool expected;
83+
} compare_check;
84+
85+
86+
static void
87+
test_mongoc_matcher_compare (void)
88+
{
89+
mongoc_matcher_t *matcher;
90+
compare_check checks[] = {
91+
{ "$gt", 2, 2, false },
92+
{ "$gte", 2, 2, true},
93+
{ "$lt", 2, 2, false},
94+
{ "$lte", 2, 2, true},
95+
{ "$ne", 2, 2, false},
96+
{ NULL }
97+
};
98+
bson_t *doc;
99+
bson_t *q;
100+
int i;
101+
102+
for (i = 0; checks [i].op; i++) {
103+
doc = BCON_NEW ("a", BCON_INT32 (checks[i].doc));
104+
q = BCON_NEW ("a", "{", checks[i].op, BCON_INT32 (checks[i].query), "}");
105+
matcher = mongoc_matcher_new (q, NULL);
106+
assert (matcher);
107+
assert (mongoc_matcher_match (matcher, doc) == checks[i].expected);
108+
bson_destroy (q);
109+
bson_destroy (doc);
110+
mongoc_matcher_destroy (matcher);
111+
}
112+
}
113+
114+
77115
static void
78116
test_mongoc_matcher_bad_spec (void)
79117
{
@@ -265,6 +303,7 @@ void
265303
test_matcher_install (TestSuite *suite)
266304
{
267305
TestSuite_Add (suite, "/Matcher/basic", test_mongoc_matcher_basic);
306+
TestSuite_Add (suite, "/Matcher/compare", test_mongoc_matcher_compare);
268307
TestSuite_Add (suite, "/Matcher/bad_spec", test_mongoc_matcher_bad_spec);
269308
TestSuite_Add (suite, "/Matcher/eq/utf8", test_mongoc_matcher_eq_utf8);
270309
TestSuite_Add (suite, "/Matcher/eq/int32", test_mongoc_matcher_eq_int32);

0 commit comments

Comments
 (0)