Advanced Chunk Processing Library 0.2.0
A comprehensive C++ library for advanced data chunking strategies and processing operations
Loading...
Searching...
No Matches
sub_chunk_strategies_test.cpp
Go to the documentation of this file.
1/**
2 * @file sub_chunk_strategies_test.cpp
3 * @brief Test suite for sub-chunking strategies
4 *
5 * This file contains comprehensive tests for:
6 * - Recursive sub-chunking
7 * - Hierarchical sub-chunking
8 * - Conditional sub-chunking
9 * - Edge cases and error conditions
10 */
11#include "chunk_strategies.hpp"
12#include "test_base.hpp"
13#include <gtest/gtest.h>
14#include <iostream>
15#include <memory>
16#include <vector>
17
18using namespace chunk_processing;
19
20/**
21 * @brief Test fixture for sub-chunking strategy tests
22 *
23 * Provides common test data and setup for all sub-chunking tests
24 */
26protected:
27 std::vector<double> test_data;
28
29 void SetUp() override {
31
32 test_data = {1.0, 1.1, 1.2, 5.0, 5.1, 5.2, 2.0, 2.1, 2.2,
33 6.0, 6.1, 6.2, 3.0, 3.1, 3.2, 7.0, 7.1, 7.2};
34 }
35
36 void TearDown() override {
37 test_data.clear();
39 }
40};
41
42TEST_F(SubChunkStrategiesTest, RecursiveStrategyTest) {
43 std::unique_lock<std::mutex> lock(global_test_mutex_);
44
45 auto variance_strategy = std::make_shared<chunk_processing::VarianceStrategy<double>>(3.0);
46 ASSERT_TRUE(is_valid_resource(variance_strategy));
47
48 chunk_processing::RecursiveSubChunkStrategy<double> recursive_strategy(variance_strategy, 3, 2);
49 auto result = recursive_strategy.apply(test_data);
50
51 lock.unlock();
52
53 ASSERT_GT(result.size(), 0);
54 for (const auto& chunk : result) {
55 ASSERT_GE(chunk.size(), 2);
56 }
57}
58
59TEST_F(SubChunkStrategiesTest, HierarchicalStrategyTest) {
60 try {
61 // Create and validate strategies
62 auto variance_strategy = std::make_shared<chunk_processing::VarianceStrategy<double>>(5.0);
63 auto entropy_strategy = std::make_shared<chunk_processing::EntropyStrategy<double>>(1.0);
64
65 if (!variance_strategy || !entropy_strategy) {
66 FAIL() << "Failed to create strategies";
67 }
68
69 // Create strategy vector
70 std::vector<std::shared_ptr<chunk_processing::ChunkStrategy<double>>> strategies;
71 strategies.reserve(2); // Pre-reserve space
72 strategies.push_back(variance_strategy);
73 strategies.push_back(entropy_strategy);
74
75 // Create hierarchical strategy
76 chunk_processing::HierarchicalSubChunkStrategy<double> hierarchical_strategy(strategies, 2);
77
78 // Apply strategy and validate results
79 auto result = hierarchical_strategy.apply(test_data);
80 ASSERT_GT(result.size(), 0) << "Result should not be empty";
81
82 // Validate chunk sizes
83 for (const auto& chunk : result) {
84 ASSERT_GE(chunk.size(), 2) << "Chunk size should be at least 2";
85 }
86 } catch (const std::exception& e) {
87 FAIL() << "Exception thrown: " << e.what();
88 }
89}
90
91TEST_F(SubChunkStrategiesTest, ConditionalStrategyTest) {
92 try {
93 // Create condition function
94 auto condition = [](const std::vector<double>& chunk) { return chunk.size() > 4; };
95
96 // Create and validate base strategy
97 auto variance_strategy = std::make_shared<chunk_processing::VarianceStrategy<double>>(5.0);
98 if (!variance_strategy) {
99 FAIL() << "Failed to create variance strategy";
100 }
101
102 // Create conditional strategy
104 variance_strategy, condition, 2);
105
106 // Apply strategy and validate results
107 auto result = conditional_strategy.apply(test_data);
108 ASSERT_GT(result.size(), 0) << "Result should not be empty";
109
110 // Validate chunk sizes
111 for (const auto& chunk : result) {
112 ASSERT_GE(chunk.size(), 2) << "Chunk size should be at least 2";
113 }
114 } catch (const std::exception& e) {
115 FAIL() << "Exception thrown: " << e.what();
116 }
117}
118
120 try {
121 std::vector<double> empty_data;
122
123 // Create and validate base strategy
124 auto variance_strategy = std::make_shared<chunk_processing::VarianceStrategy<double>>(3.0);
125 if (!variance_strategy) {
126 FAIL() << "Failed to create variance strategy";
127 }
128
129 // Test recursive strategy
130 {
132 variance_strategy, 2, 2);
133 auto result = recursive_strategy.apply(empty_data);
134 EXPECT_TRUE(result.empty())
135 << "Recursive strategy should return empty result for empty data";
136 }
137
138 // Test hierarchical strategy
139 {
140 std::vector<std::shared_ptr<chunk_processing::ChunkStrategy<double>>> strategies{
141 variance_strategy};
142 chunk_processing::HierarchicalSubChunkStrategy<double> hierarchical_strategy(strategies,
143 2);
144 auto result = hierarchical_strategy.apply(empty_data);
145 EXPECT_TRUE(result.empty())
146 << "Hierarchical strategy should return empty result for empty data";
147 }
148
149 // Test conditional strategy
150 {
151 auto condition = [](const std::vector<double>& chunk) { return chunk.size() > 4; };
153 variance_strategy, condition, 2);
154 auto result = conditional_strategy.apply(empty_data);
155 EXPECT_TRUE(result.empty())
156 << "Conditional strategy should return empty result for empty data";
157 }
158 } catch (const std::exception& e) {
159 FAIL() << "Exception thrown: " << e.what();
160 }
161}
Defines various strategies for chunking data based on different criteria.
void SetUp() override
Definition test_base.cpp:8
void TearDown() override
Definition test_base.cpp:15
Test fixture for sub-chunking strategy tests.
std::vector< std::vector< T > > apply(const std::vector< T > &data) const override
std::vector< std::vector< T > > apply(const std::vector< T > &data) const override
std::vector< std::vector< T > > apply(const std::vector< T > &data) const override
TEST_F(SubChunkStrategiesTest, RecursiveStrategyTest)