| 
 | 1 | +/**  | 
 | 2 | + * @file   unit_partitioned_matrix.cc  | 
 | 3 | + *  | 
 | 4 | + * @section LICENSE  | 
 | 5 | + *  | 
 | 6 | + * The MIT License  | 
 | 7 | + *  | 
 | 8 | + * @copyright Copyright (c) 2024 TileDB, Inc.  | 
 | 9 | + *  | 
 | 10 | + * Permission is hereby granted, free of charge, to any person obtaining a copy  | 
 | 11 | + * of this software and associated documentation files (the "Software"), to deal  | 
 | 12 | + * in the Software without restriction, including without limitation the rights  | 
 | 13 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  | 
 | 14 | + * copies of the Software, and to permit persons to whom the Software is  | 
 | 15 | + * furnished to do so, subject to the following conditions:  | 
 | 16 | + *  | 
 | 17 | + * The above copyright notice and this permission notice shall be included in  | 
 | 18 | + * all copies or substantial portions of the Software.  | 
 | 19 | + *  | 
 | 20 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  | 
 | 21 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  | 
 | 22 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  | 
 | 23 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  | 
 | 24 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  | 
 | 25 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN  | 
 | 26 | + * THE SOFTWARE.  | 
 | 27 | + *  | 
 | 28 | + * @section DESCRIPTION  | 
 | 29 | + *  | 
 | 30 | + */  | 
 | 31 | + | 
 | 32 | +#include <algorithm>  | 
 | 33 | +#include <catch2/catch_all.hpp>  | 
 | 34 | +#include <vector>  | 
 | 35 | +#include "cpos.h"  | 
 | 36 | +#include "detail/linalg/partitioned_matrix.h"  | 
 | 37 | +#include "mdspan/mdspan.hpp"  | 
 | 38 | + | 
 | 39 | +TEST_CASE("partitioned_matrix: test test", "[partitioned_matrix]") {  | 
 | 40 | +  REQUIRE(true);  | 
 | 41 | +}  | 
 | 42 | + | 
 | 43 | +TEST_CASE("partitioned_matrix: sizes constructor", "[partitioned_matrix]") {  | 
 | 44 | +  using feature_type = int;  | 
 | 45 | +  using id_type = int;  | 
 | 46 | +  using part_index_type = int;  | 
 | 47 | +  size_t dimensions = 3;  | 
 | 48 | +  size_t max_num_vectors = 5;  | 
 | 49 | +  size_t max_num_partitions = 2;  | 
 | 50 | + | 
 | 51 | +  auto partitioned_matrix =  | 
 | 52 | +      ColMajorPartitionedMatrix<feature_type, id_type, part_index_type>(  | 
 | 53 | +          dimensions, max_num_vectors, max_num_partitions);  | 
 | 54 | +  CHECK(partitioned_matrix.num_vectors() == 0);  | 
 | 55 | +  CHECK(partitioned_matrix.num_partitions() == 0);  | 
 | 56 | +  CHECK(std::equal(  | 
 | 57 | +      partitioned_matrix.ids().begin(),  | 
 | 58 | +      partitioned_matrix.ids().end(),  | 
 | 59 | +      std::vector<part_index_type>{0, 0, 0, 0, 0}.begin()));  | 
 | 60 | +  CHECK(std::equal(  | 
 | 61 | +      partitioned_matrix.indices().begin(),  | 
 | 62 | +      partitioned_matrix.indices().end(),  | 
 | 63 | +      std::vector<part_index_type>{0, 0, 0}.begin()));  | 
 | 64 | + | 
 | 65 | +  CHECK(partitioned_matrix.load() == false);  | 
 | 66 | +  CHECK(partitioned_matrix.num_vectors() == 0);  | 
 | 67 | +  CHECK(partitioned_matrix.num_partitions() == 0);  | 
 | 68 | +  CHECK(std::equal(  | 
 | 69 | +      partitioned_matrix.ids().begin(),  | 
 | 70 | +      partitioned_matrix.ids().end(),  | 
 | 71 | +      std::vector<part_index_type>{0, 0, 0, 0, 0}.begin()));  | 
 | 72 | +  CHECK(std::equal(  | 
 | 73 | +      partitioned_matrix.indices().begin(),  | 
 | 74 | +      partitioned_matrix.indices().end(),  | 
 | 75 | +      std::vector<part_index_type>{0, 0, 0}.begin()));  | 
 | 76 | +}  | 
 | 77 | + | 
 | 78 | +TEST_CASE("partitioned_matrix: vectors constructor", "[partitioned_matrix]") {  | 
 | 79 | +  using feature_type = float;  | 
 | 80 | +  using id_type = float;  | 
 | 81 | +  using part_index_type = float;  | 
 | 82 | + | 
 | 83 | +  auto parts =  | 
 | 84 | +      ColMajorMatrix<feature_type>{{1, 1, 1}, {2, 2, 2}, {3, 3, 3}, {4, 4, 4}};  | 
 | 85 | +  std::vector<id_type> ids = {1, 2, 3, 4};  | 
 | 86 | +  std::vector<part_index_type> part_index = {0, 1, 4};  | 
 | 87 | + | 
 | 88 | +  auto partitioned_matrix =  | 
 | 89 | +      ColMajorPartitionedMatrix<feature_type, id_type, part_index_type>(  | 
 | 90 | +          parts, ids, part_index);  | 
 | 91 | + | 
 | 92 | +  CHECK(partitioned_matrix.num_vectors() == 4);  | 
 | 93 | +  CHECK(partitioned_matrix.num_partitions() == 2);  | 
 | 94 | +  CHECK(std::equal(  | 
 | 95 | +      partitioned_matrix.ids().begin(),  | 
 | 96 | +      partitioned_matrix.ids().end(),  | 
 | 97 | +      std::vector<part_index_type>{1, 2, 3, 4}.begin()));  | 
 | 98 | +  CHECK(std::equal(  | 
 | 99 | +      partitioned_matrix.indices().begin(),  | 
 | 100 | +      partitioned_matrix.indices().end(),  | 
 | 101 | +      std::vector<part_index_type>{0, 1, 4}.begin()));  | 
 | 102 | + | 
 | 103 | +  CHECK(partitioned_matrix.load() == false);  | 
 | 104 | +  CHECK(partitioned_matrix.num_vectors() == 4);  | 
 | 105 | +  CHECK(partitioned_matrix.num_partitions() == 2);  | 
 | 106 | +  CHECK(std::equal(  | 
 | 107 | +      partitioned_matrix.ids().begin(),  | 
 | 108 | +      partitioned_matrix.ids().end(),  | 
 | 109 | +      std::vector<part_index_type>{1, 2, 3, 4}.begin()));  | 
 | 110 | +  CHECK(std::equal(  | 
 | 111 | +      partitioned_matrix.indices().begin(),  | 
 | 112 | +      partitioned_matrix.indices().end(),  | 
 | 113 | +      std::vector<part_index_type>{0, 1, 4}.begin()));  | 
 | 114 | +}  | 
 | 115 | + | 
 | 116 | +TEST_CASE("partitioned_matrix: training constructor", "[partitioned_matrix]") {  | 
 | 117 | +  using feature_type = uint64_t;  | 
 | 118 | +  using id_type = uint64_t;  | 
 | 119 | +  using part_index_type = uint64_t;  | 
 | 120 | + | 
 | 121 | +  auto training_set =  | 
 | 122 | +      ColMajorMatrix<feature_type>{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};  | 
 | 123 | +  std::vector<id_type> part_labels = {1, 0, 1, 0, 1};  | 
 | 124 | +  size_t num_parts = 2;  | 
 | 125 | + | 
 | 126 | +  auto partitioned_matrix =  | 
 | 127 | +      ColMajorPartitionedMatrix<feature_type, id_type, part_index_type>(  | 
 | 128 | +          training_set, part_labels, num_parts);  | 
 | 129 | +  CHECK(partitioned_matrix.num_vectors() == _cpo::num_vectors(training_set));  | 
 | 130 | +  CHECK(partitioned_matrix.num_partitions() == num_parts);  | 
 | 131 | +  CHECK(std::equal(  | 
 | 132 | +      partitioned_matrix.data(),  | 
 | 133 | +      partitioned_matrix.data() + partitioned_matrix.num_vectors() *  | 
 | 134 | +                                      _cpo::dimensions(partitioned_matrix),  | 
 | 135 | +      std::vector<feature_type>{2, 2, 4, 4, 1, 1, 3, 3, 5, 5}.begin()));  | 
 | 136 | +  CHECK(std::equal(  | 
 | 137 | +      partitioned_matrix.ids().begin(),  | 
 | 138 | +      partitioned_matrix.ids().end(),  | 
 | 139 | +      std::vector<part_index_type>{1, 3, 0, 2, 4}.begin()));  | 
 | 140 | +  CHECK(std::equal(  | 
 | 141 | +      partitioned_matrix.indices().begin(),  | 
 | 142 | +      partitioned_matrix.indices().end(),  | 
 | 143 | +      std::vector<part_index_type>{0, 2, 5}.begin()));  | 
 | 144 | +}  | 
0 commit comments