| 1 | /**************************************************************************/ |
| 2 | /* test_vset.h */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #pragma once |
| 32 | |
| 33 | #include "core/templates/vset.h" |
| 34 | |
| 35 | #include "tests/test_macros.h" |
| 36 | |
| 37 | namespace TestVSet { |
| 38 | |
| 39 | template <typename T> |
| 40 | class TestClass : public VSet<T> { |
| 41 | public: |
| 42 | int _find(const T &p_val, bool &r_exact) const { |
| 43 | return VSet<T>::_find(p_val, r_exact); |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | TEST_CASE("[VSet] _find and _find_exact correctness.") { |
| 48 | TestClass<int> set; |
| 49 | |
| 50 | // insert some values |
| 51 | set.insert(10); |
| 52 | set.insert(20); |
| 53 | set.insert(30); |
| 54 | set.insert(40); |
| 55 | set.insert(50); |
| 56 | |
| 57 | // data should be sorted |
| 58 | CHECK(set.size() == 5); |
| 59 | CHECK(set[0] == 10); |
| 60 | CHECK(set[1] == 20); |
| 61 | CHECK(set[2] == 30); |
| 62 | CHECK(set[3] == 40); |
| 63 | CHECK(set[4] == 50); |
| 64 | |
| 65 | // _find_exact return exact position for existing elements |
| 66 | CHECK(set.find(10) == 0); |
| 67 | CHECK(set.find(30) == 2); |
| 68 | CHECK(set.find(50) == 4); |
| 69 | |
| 70 | // _find_exact return -1 for non-existing elements |
| 71 | CHECK(set.find(15) == -1); |
| 72 | CHECK(set.find(0) == -1); |
| 73 | CHECK(set.find(60) == -1); |
| 74 | |
| 75 | // test _find |
| 76 | bool exact; |
| 77 | |
| 78 | // existing elements |
| 79 | CHECK(set._find(10, exact) == 0); |
| 80 | CHECK(exact == true); |
| 81 | |
| 82 | CHECK(set._find(30, exact) == 2); |
| 83 | CHECK(exact == true); |
| 84 | |
| 85 | // non-existing elements |
| 86 | CHECK(set._find(25, exact) == 2); |
| 87 | CHECK(exact == false); |
| 88 | |
| 89 | CHECK(set._find(35, exact) == 3); |
| 90 | CHECK(exact == false); |
| 91 | |
| 92 | CHECK(set._find(5, exact) == 0); |
| 93 | CHECK(exact == false); |
| 94 | |
| 95 | CHECK(set._find(60, exact) == 5); |
| 96 | CHECK(exact == false); |
| 97 | } |
| 98 | |
| 99 | } // namespace TestVSet |