• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

realm / realm-core / nicola.cabiddu_1040

26 Sep 2023 05:08PM UTC coverage: 91.056% (-1.9%) from 92.915%
nicola.cabiddu_1040

Pull #6766

Evergreen

nicola-cab
several fixes and final client reset algo for collection in mixed
Pull Request #6766: Client Reset for collections in mixed / nested collections

97128 of 178458 branches covered (0.0%)

1524 of 1603 new or added lines in 5 files covered. (95.07%)

4511 existing lines in 109 files now uncovered.

236619 of 259862 relevant lines covered (91.06%)

7169640.31 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.03
/test/test_array_mixed.cpp
1
/*************************************************************************
2
 *
3
 * Copyright 2018 Realm Inc.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 **************************************************************************/
18

19
#include "testsettings.hpp"
20

21
#include <realm.hpp>
22
#include <realm/array_mixed.hpp>
23

24
#include "test.hpp"
25

26
using namespace realm;
27
using namespace realm::test_util;
28

29
// Test independence and thread-safety
30
// -----------------------------------
31
//
32
// All tests must be thread safe and independent of each other. This
33
// is required because it allows for both shuffling of the execution
34
// order and for parallelized testing.
35
//
36
// In particular, avoid using std::rand() since it is not guaranteed
37
// to be thread safe. Instead use the API offered in
38
// `test/util/random.hpp`.
39
//
40
// All files created in tests must use the TEST_PATH macro (or one of
41
// its friends) to obtain a suitable file system path. See
42
// `test/util/test_path.hpp`.
43
//
44
//
45
// Debugging and the ONLY() macro
46
// ------------------------------
47
//
48
// A simple way of disabling all tests except one called `Foo`, is to
49
// replace TEST(Foo) with ONLY(Foo) and then recompile and rerun the
50
// test suite. Note that you can also use filtering by setting the
51
// environment varible `UNITTEST_FILTER`. See `README.md` for more on
52
// this.
53
//
54
// Another way to debug a particular test, is to copy that test into
55
// `experiments/testcase.cpp` and then run `sh build.sh
56
// check-testcase` (or one of its friends) from the command line.
57

58
#ifdef TEST_ARRAY_MIXED
59

60
TEST(ArrayMixed_Basics)
61
{
2✔
62
    ArrayMixed arr(Allocator::get_default());
2✔
63
    arr.create();
2✔
64
    arr.add(int64_t(5));
2✔
65
    arr.add(true);
2✔
66
    arr.add(3.5f);
2✔
67
    arr.add(17.87);
2✔
68
    arr.add("Goodbye cruel world");
2✔
69
    arr.add("Goodbye yellow brick road");
2✔
70
    std::string bin(42, 'x');
2✔
71
    arr.add(BinaryData(bin.data(), bin.size()));
2✔
72
    arr.add(Timestamp(1234, 5678));
2✔
73
    arr.add({});
2✔
74
    arr.add(Timestamp(2345, 6789));
2✔
75
    arr.add(Decimal128("10.50"));
2✔
76
    arr.add(ObjectId("abcdefabcdefabcdefabcdef"));
2✔
77

1✔
78
    CHECK_EQUAL(arr.size(), 12);
2✔
79
    CHECK_EQUAL(arr.get(0).get_int(), 5);
2✔
80
    CHECK_EQUAL(arr.get(1).get_bool(), true);
2✔
81
    CHECK_EQUAL(arr.get(2).get_float(), 3.5f);
2✔
82
    CHECK_EQUAL(arr.get(3).get_double(), 17.87);
2✔
83
    CHECK_EQUAL(arr.get(4).get_string(), "Goodbye cruel world");
2✔
84
    CHECK_EQUAL(arr.get(5).get_string(), "Goodbye yellow brick road");
2✔
85
    CHECK_EQUAL(arr.get(6).get_binary(), BinaryData(bin.data(), bin.size()));
2✔
86
    CHECK_EQUAL(arr.get(7).get_timestamp(), Timestamp(1234, 5678));
2✔
87
    CHECK(arr.is_null(8));
2✔
88
    CHECK(arr.get(8).is_null());
2✔
89
    CHECK_EQUAL(arr.get(9).get_timestamp(), Timestamp(2345, 6789));
2✔
90
    CHECK_EQUAL(arr.get(10).get<Decimal128>(), Decimal128("10.50"));
2✔
91
    CHECK_EQUAL(arr.get(11).get<ObjectId>(), ObjectId("abcdefabcdefabcdefabcdef"));
2✔
92

1✔
93
    CHECK_NOT(arr.get(4) == arr.get(5));
2✔
94

1✔
95
    arr.set(4, -177); // Replace string with int
2✔
96
    CHECK_EQUAL(arr.get(4).get_int(), -177);
2✔
97
    CHECK_EQUAL(arr.get(5).get_string(), "Goodbye yellow brick road");
2✔
98
    CHECK_EQUAL(arr.get(6).get_binary(), BinaryData(bin.data(), bin.size()));
2✔
99

1✔
100
    CHECK_EQUAL(arr.find_first("Goodbye yellow brick road"), 5);
2✔
101

1✔
102
    arr.erase(5); // Erase string
2✔
103
    CHECK_EQUAL(arr.get(5).get_binary(), BinaryData(bin.data(), bin.size()));
2✔
104

1✔
105
    arr.insert(2, Mixed());    // null
2✔
106
    arr.insert(2, int64_t(4500000000)); // Requires more than 32 bit
2✔
107

1✔
108
    CHECK_EQUAL(arr.get(2).get_int(), 4500000000);
2✔
109
    CHECK(arr.is_null(3));
2✔
110

1✔
111
    arr.set(8, Mixed()); // null replaces Timestamp
2✔
112
    CHECK_EQUAL(arr.get(10).get_timestamp(), Timestamp(2345, 6789));
2✔
113

1✔
114
    arr.set(4, 123.456); // double replaces float
2✔
115
    CHECK_EQUAL(arr.get(4).get_double(), 123.456);
2✔
116
    CHECK_EQUAL(arr.get(2).get_int(), 4500000000);
2✔
117

1✔
118
    ArrayMixed arr2(Allocator::get_default());
2✔
119
    arr2.create();
2✔
120

1✔
121
    arr.move(arr2, 4);
2✔
122
    CHECK_EQUAL(arr.size(), 4);
2✔
123
    CHECK_EQUAL(arr2.size(), 9);
2✔
124
    CHECK_EQUAL(arr.get(0).get_int(), 5);
2✔
125
    CHECK_EQUAL(arr.get(1).get_bool(), true);
2✔
126
    CHECK_EQUAL(arr.get(2).get_int(), 4500000000);
2✔
127
    CHECK(arr.is_null(3));
2✔
128

1✔
129
    /*
1✔
130
    for (size_t i = 0; i < 7; i++)
1✔
131
        std::cout << arr2.get(i) << std::endl;
1✔
132
    */
1✔
133

1✔
134
    CHECK_EQUAL(arr2.get(0).get_double(), 123.456);
2✔
135
    CHECK_EQUAL(arr2.get(1).get_double(), 17.87);
2✔
136
    CHECK_EQUAL(arr2.get(2).get_int(), -177);
2✔
137
    CHECK_EQUAL(arr2.get(3).get_binary(), BinaryData(bin.data(), bin.size()));
2✔
138
    CHECK(arr2.is_null(4));
2✔
139
    CHECK(arr2.is_null(5));
2✔
140
    CHECK_EQUAL(arr2.get(6).get_timestamp(), Timestamp(2345, 6789));
2✔
141

1✔
142
    arr2.clear();
2✔
143
    CHECK_EQUAL(arr2.size(), 0);
2✔
144
    arr2.clear(); // Check idempotency
2✔
145
    CHECK_EQUAL(arr2.size(), 0);
2✔
146
    arr2.add("Hello");
2✔
147
    CHECK_EQUAL(arr2.size(), 1);
2✔
148

1✔
149
    arr.destroy();
2✔
150
    arr2.destroy();
2✔
151
}
2✔
152

153
TEST(Mixed_Table)
154
{
2✔
155
    Table t;
2✔
156
    auto col_data = t.add_column(type_Mixed, "data");
2✔
157
    auto obj0 = t.create_object().set(col_data, Mixed(5));
2✔
158
    auto obj1 = t.create_object().set(col_data, Mixed("Hello"));
2✔
159
    CHECK_EQUAL(obj0.get_any(col_data), Mixed(5));
2✔
160
    CHECK_EQUAL(obj1.get_any(col_data), Mixed("Hello"));
2✔
161
    CHECK_EQUAL(obj0.get_any("data"), Mixed(5));
2✔
162
    CHECK_EQUAL(obj1.get_any("data"), Mixed("Hello"));
2✔
163
}
2✔
164

165

166
TEST(Mixed_SortNumeric)
167
{
2✔
168
    Table t;
2✔
169
    auto col_data = t.add_column(type_Mixed, "data");
2✔
170
    t.create_object().set(col_data, Mixed(5));
2✔
171
    t.create_object().set(col_data, Mixed(false));
2✔
172
    t.create_object().set(col_data, Mixed(-258));
2✔
173
    t.create_object().set(col_data, Mixed(256.25f));
2✔
174
    t.create_object().set(col_data, Mixed(34.8));
2✔
175
    t.create_object().set(col_data, Mixed(Decimal128("-500")));
2✔
176
    t.create_object().set(col_data, Mixed(7.5f));
2✔
177
    t.create_object().set(col_data, Mixed(500));
2✔
178
    t.create_object().set(col_data, Mixed(Decimal128("129.85")));
2✔
179
    t.create_object().set(col_data, Mixed());
2✔
180
    t.create_object().set(col_data, Mixed(100));
2✔
181
    t.create_object().set(col_data, Mixed("Hello"));
2✔
182
    t.create_object().set(col_data, Mixed(42));
2✔
183
    t.create_object().set(col_data, Mixed(0.001f));
2✔
184
    t.create_object().set(col_data, Mixed(-278987.9));
2✔
185
    t.create_object().set(col_data, Mixed(Decimal128("10000")));
2✔
186
    t.create_object().set(col_data, Mixed(true));
2✔
187
    t.create_object().set(col_data, Mixed(42.125f));
2✔
188

1✔
189
    auto tv = t.where().find_all();
2✔
190
    auto sz = tv.size();
2✔
191
    CHECK_EQUAL(tv.size(), 18);
2✔
192
    tv.sort(col_data);
2✔
193
    std::ostringstream out;
2✔
194
    out.precision(8);
2✔
195
    std::string expected = "null\nfalse\ntrue\n-278987.9\n-500\n-258\n0.001\n"
2✔
196
                           "5\n7.5\n34.8\n42\n42.125\n100\n"
2✔
197
                           "129.85\n256.25\n500\n10000\n\"Hello\"\n";
2✔
198
    for (size_t i = 0; i < sz; i++) {
38✔
199
        Mixed val = tv.get_object(i).get<Mixed>(col_data);
36✔
200
        out << val << std::endl;
36✔
201
    }
36✔
202
    std::string actual = out.str();
2✔
203
    CHECK_EQUAL(actual, expected);
2✔
204
}
2✔
205

206
TEST(Mixed_Compare)
207
{
2✔
208
    CHECK(Mixed(false) < Mixed(true));
2✔
209
    CHECK(Mixed(int64_t(0x1234567812345678)) > Mixed(1.311768e18)); // Large int
2✔
210
    CHECK(Mixed(int64_t(0x1234567812345678)) < Mixed(1.e19));       // double larger than largest int
2✔
211
    CHECK(Mixed(0 - int64_t(0x1234567812345678)) > Mixed(-1.e19));  // double more negative than most negative int
2✔
212
    CHECK(Mixed(nan("123")) < 5);
2✔
213

1✔
214
    CHECK_EQUAL(Mixed(double(2.2f)), Mixed(2.2f));
2✔
215
    CHECK_EQUAL(Mixed(2.2f), Mixed(double(2.2f)));
2✔
216
    CHECK_EQUAL(Mixed(Decimal128(2.2)), Mixed(2.2));
2✔
217
    CHECK_EQUAL(Mixed(Decimal128(2.2f)), Mixed(2.2f));
2✔
218
    CHECK_EQUAL(Mixed(2.2), Mixed(Decimal128(2.2)));
2✔
219
    CHECK_EQUAL(Mixed(2.2f), Mixed(Decimal128(2.2f)));
2✔
220

1✔
221
    std::string str("Hello");
2✔
222
    CHECK(Mixed(str) != Mixed(BinaryData(str)));
2✔
223
    CHECK_NOT(Mixed::types_are_comparable(Mixed(), Mixed()));
2✔
224
    CHECK(Mixed() == Mixed());
2✔
225
    CHECK(Mixed(0.f) < Mixed(1));
2✔
226
    CHECK(Mixed(1) < Mixed("a"));
2✔
227
    CHECK(Mixed(0.f) < Mixed("a"));
2✔
228
    CHECK(Mixed(10.0) < Mixed(BinaryData("b")));
2✔
229
    CHECK(Mixed("a") < Mixed(BinaryData("b")));
2✔
230
    CHECK(Mixed("c") < Mixed(BinaryData("b")));
2✔
231
    CHECK(Mixed("a") < Mixed(Timestamp(1, 2)));
2✔
232
    CHECK(Mixed(Decimal128("25")) < Mixed(Timestamp(1, 2)));
2✔
233
    CHECK(Mixed(Timestamp(2, 3)) < Mixed(ObjectId(Timestamp(1, 2), 0, 0))); // Not value comparable
2✔
234
}
2✔
235

236
TEST(Mixed_string_binary_compare)
237
{
2✔
238
    const auto a = Mixed("a");
2✔
239
    const auto b = Mixed("B");
2✔
240
    const auto c = Mixed(BinaryData("C", 1));
2✔
241

1✔
242
    CHECK_LESS(a.compare(b), 0);
2✔
243
    CHECK_LESS(b.compare(c), 0);
2✔
244
    CHECK_GREATER(c.compare(a), 0);
2✔
245

1✔
246
    std::vector<std::array<Mixed, 3>> perms = {
2✔
247
        {a, b, c}, {a, c, b}, {b, a, c}, // vars[0] is the expected ordering
2✔
248
        {b, c, a}, {c, a, b}, {c, b, a},
2✔
249
    };
2✔
250
    for (auto& arr : perms) {
12✔
251
        std::sort(arr.begin(), arr.end());
12✔
252
        CHECK_EQUAL(arr[0], perms[0][0]);
12✔
253
        CHECK_EQUAL(arr[1], perms[0][1]);
12✔
254
        CHECK_EQUAL(arr[2], perms[0][2]);
12✔
255
    }
12✔
256
}
2✔
257

258
// This test takes ~10 minutes in a Release build so it's disabled by default
259
TEST_IF(Mixed_Compare_Float_Decimal128, false)
UNCOV
260
{
×
UNCOV
261
    static_assert(sizeof(float) == sizeof(uint32_t));
×
262
    // Test every non-NaN, non-Inf float by iterating over every possible bit pattern
UNCOV
263
    for (uint64_t i = 0; i <= std::numeric_limits<uint32_t>::max(); ++i) {
×
UNCOV
264
        auto j = static_cast<uint32_t>(i);
×
UNCOV
265
        float f;
×
UNCOV
266
        memcpy(&f, &j, sizeof(j));
×
UNCOV
267
        if (!std::isfinite(f))
×
UNCOV
268
            continue;
×
UNCOV
269
        Mixed m1{f};
×
UNCOV
270
        Mixed m2{Decimal128(f)};
×
UNCOV
271
        CHECK_EQUAL(m1, m2);
×
UNCOV
272
        CHECK_EQUAL(m2, m1);
×
UNCOV
273
    }
×
UNCOV
274
}
×
275

276
#endif // TEST_ARRAY_VARIANT
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc