vstring_ ======== .. cpp:class:: numpy::vstring_ numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use vstring_ vstring_ obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``vstring\_(const char \*s)`` - NP_VARIABLE_STRING.H:18 - :ref:`View ` * - ``vstring\_(const std::string &s)`` - NP_VARIABLE_STRING.H:20 - :ref:`View ` * - ``noexcept vstring\_(std::string &&s)`` - NP_VARIABLE_STRING.H:22 - :ref:`View ` * - ``vstring\_(const vstring\_ &other)`` - NP_VARIABLE_STRING.H:25 - :ref:`View ` * - ``noexcept vstring\_(vstring\_ &&other)`` - NP_VARIABLE_STRING.H:28 - :ref:`View ` Operators --------- .. list-table:: :widths: 40 25 15 20 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``vstring\_ & operator=(const vstring\_ &other)`` - vstring\_ & - NP_VARIABLE_STRING.H:31 - * - ``vstring\_ & noexcept operator=(vstring\_ &&other)`` - vstring\_ & noexcept - NP_VARIABLE_STRING.H:38 - * - ``vstring\_ & operator=(const char \*s)`` - vstring\_ & - NP_VARIABLE_STRING.H:45 - * - ``vstring\_ & operator=(const std::string &s)`` - vstring\_ & - NP_VARIABLE_STRING.H:50 - * - ``vstring\_ & noexcept operator=(std::string &&s)`` - vstring\_ & noexcept - NP_VARIABLE_STRING.H:55 - * - ``char & operator[](size_tpos)`` - char & - NP_VARIABLE_STRING.H:69 - * - ``const char & operator[](size_tpos)`` - const char & - NP_VARIABLE_STRING.H:70 - * - ``vstring\_ operator+(const vstring\_ &other)`` - vstring\_ - NP_VARIABLE_STRING.H:119 - * - ``vstring\_ & operator+=(const vstring\_ &other)`` - vstring\_ & - NP_VARIABLE_STRING.H:123 - * - ``vstring\_ & operator+=(const char \*s)`` - vstring\_ & - NP_VARIABLE_STRING.H:128 - * - ``vstring\_ & operator+=(charc)`` - vstring\_ & - NP_VARIABLE_STRING.H:133 - * - ``bool operator==(const vstring\_ &other)`` - bool - NP_VARIABLE_STRING.H:192 - * - ``bool operator!=(const vstring\_ &other)`` - bool - NP_VARIABLE_STRING.H:196 - * - ``bool operator<(const vstring\_ &other)`` - bool - NP_VARIABLE_STRING.H:200 - * - ``bool operator<=(const vstring\_ &other)`` - bool - NP_VARIABLE_STRING.H:204 - * - ``bool operator>(const vstring\_ &other)`` - bool - NP_VARIABLE_STRING.H:208 - * - ``bool operator>=(const vstring\_ &other)`` - bool - NP_VARIABLE_STRING.H:212 - * - ``bool operator==(const char \*s)`` - bool - NP_VARIABLE_STRING.H:217 - * - ``bool operator!=(const char \*s)`` - bool - NP_VARIABLE_STRING.H:221 - * - ``bool operator==(const std::string &s)`` - bool - NP_VARIABLE_STRING.H:226 - * - ``bool operator!=(const std::string &s)`` - bool - NP_VARIABLE_STRING.H:230 - Array Creation -------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool empty()`` - bool - NP_VARIABLE_STRING.H:64 - :ref:`View ` Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``char & at(size_tpos)`` - char & - NP_VARIABLE_STRING.H:72 - :ref:`View ` * - ``const char & at(size_tpos)`` - const char & - NP_VARIABLE_STRING.H:73 - :ref:`View ` * - ``char at_safe(size_tpos)`` - char - NP_VARIABLE_STRING.H:93 - :ref:`View ` Shape Manipulation ------------------ .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``void resize(size_tnew_size)`` - void - NP_VARIABLE_STRING.H:166 - :ref:`View ` * - ``void resize(size_tnew_size, charc)`` - void - NP_VARIABLE_STRING.H:170 - :ref:`View ` Statistics ---------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``size_t max_size()`` - size_t - NP_VARIABLE_STRING.H:66 - I/O --- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::string to_string()`` - std::string - NP_VARIABLE_STRING.H:235 - :ref:`View ` Joining / Splitting ------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``void append(const vstring\_ &other)`` - void - NP_VARIABLE_STRING.H:138 - :ref:`View ` * - ``void append(const char \*s)`` - void - NP_VARIABLE_STRING.H:142 - :ref:`View ` * - ``void append(size_tn, charc)`` - void - NP_VARIABLE_STRING.H:146 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``char & back()`` - char & - NP_VARIABLE_STRING.H:78 - :ref:`View ` * - ``const char & back()`` - const char & - NP_VARIABLE_STRING.H:79 - :ref:`View ` * - ``auto begin()`` - auto - NP_VARIABLE_STRING.H:248 - :ref:`View ` * - ``auto begin()`` - auto - NP_VARIABLE_STRING.H:250 - :ref:`View ` * - ``size_t byte_size()`` - size_t - NP_VARIABLE_STRING.H:63 - :ref:`View ` * - ``const char \* c_str()`` - const char \* - NP_VARIABLE_STRING.H:239 - :ref:`View ` * - ``size_t capacity()`` - size_t - NP_VARIABLE_STRING.H:65 - * - ``auto cbegin()`` - auto - NP_VARIABLE_STRING.H:252 - * - ``auto cend()`` - auto - NP_VARIABLE_STRING.H:253 - * - ``void clear()`` - void - NP_VARIABLE_STRING.H:158 - :ref:`View ` * - ``auto crbegin()`` - auto - NP_VARIABLE_STRING.H:258 - * - ``auto crend()`` - auto - NP_VARIABLE_STRING.H:259 - * - ``const char \* data()`` - const char \* - NP_VARIABLE_STRING.H:243 - :ref:`View ` * - ``auto end()`` - auto - NP_VARIABLE_STRING.H:249 - :ref:`View ` * - ``auto end()`` - auto - NP_VARIABLE_STRING.H:251 - :ref:`View ` * - ``size_t find(const vstring\_ &str, size_tpos = 0)`` - size_t - NP_VARIABLE_STRING.H:175 - :ref:`View ` * - ``size_t find(const char \*s, size_tpos = 0)`` - size_t - NP_VARIABLE_STRING.H:179 - :ref:`View ` * - ``size_t find(charc, size_tpos = 0)`` - size_t - NP_VARIABLE_STRING.H:183 - :ref:`View ` * - ``char & front()`` - char & - NP_VARIABLE_STRING.H:75 - * - ``const char & front()`` - const char & - NP_VARIABLE_STRING.H:76 - * - ``size_t length()`` - size_t - NP_VARIABLE_STRING.H:62 - :ref:`View ` * - ``void pop_back()`` - void - NP_VARIABLE_STRING.H:154 - * - ``void push_back(charc)`` - void - NP_VARIABLE_STRING.H:150 - :ref:`View ` * - ``auto rbegin()`` - auto - NP_VARIABLE_STRING.H:254 - :ref:`View ` * - ``auto rbegin()`` - auto - NP_VARIABLE_STRING.H:256 - :ref:`View ` * - ``auto rend()`` - auto - NP_VARIABLE_STRING.H:255 - :ref:`View ` * - ``auto rend()`` - auto - NP_VARIABLE_STRING.H:257 - :ref:`View ` * - ``void reserve(size_tnew_cap)`` - void - NP_VARIABLE_STRING.H:162 - :ref:`View ` * - ``size_t rfind(const vstring\_ &str, size_tpos = std::string::npos)`` - size_t - NP_VARIABLE_STRING.H:187 - :ref:`View ` * - ``size_t size()`` - size_t - NP_VARIABLE_STRING.H:61 - :ref:`View ` * - ``vstring\_ slice(intstart, intstop = -1)`` - vstring\_ - NP_VARIABLE_STRING.H:101 - :ref:`View ` * - ``vstring\_ substr(size_tpos = 0, size_tlen = std::string::npos)`` - vstring\_ - NP_VARIABLE_STRING.H:82 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-vstring_-vstring_-0: .. dropdown:: vstring_ (np_test_1_all.cpp:8471) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8461 :emphasize-lines: 11 // Copy assignment vstring_ other("Test"); str = other; if (!(str.to_string() == "Test")) { std::string description = std::string("testVStringAssignmentVariableStrings():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(str.to_string() == \"Test\")"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Move assignment str = vstring_("Moved"); if (!(str.to_string() == "Moved")) { std::string description = std::string("testVStringAssignmentVariableStrings():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(str.to_string() == \"Moved\")"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } std::cout << " -> tests passed" << std::endl; } void testVStringOperationsVariableStrings() { std::cout << "========= testVStringOperationsVariableStrings ======================="; .. _example-vstring_-vstring_-1: .. dropdown:: vstring_ (np_test_1_all.cpp:8471) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8461 :emphasize-lines: 11 // Copy assignment vstring_ other("Test"); str = other; if (!(str.to_string() == "Test")) { std::string description = std::string("testVStringAssignmentVariableStrings():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(str.to_string() == \"Test\")"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Move assignment str = vstring_("Moved"); if (!(str.to_string() == "Moved")) { std::string description = std::string("testVStringAssignmentVariableStrings():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(str.to_string() == \"Moved\")"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } std::cout << " -> tests passed" << std::endl; } void testVStringOperationsVariableStrings() { std::cout << "========= testVStringOperationsVariableStrings ======================="; .. _example-vstring_-vstring_-2: .. dropdown:: vstring_ (np_test_1_all.cpp:8471) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8461 :emphasize-lines: 11 // Copy assignment vstring_ other("Test"); str = other; if (!(str.to_string() == "Test")) { std::string description = std::string("testVStringAssignmentVariableStrings():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(str.to_string() == \"Test\")"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Move assignment str = vstring_("Moved"); if (!(str.to_string() == "Moved")) { std::string description = std::string("testVStringAssignmentVariableStrings():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(str.to_string() == \"Moved\")"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } std::cout << " -> tests passed" << std::endl; } void testVStringOperationsVariableStrings() { std::cout << "========= testVStringOperationsVariableStrings ======================="; .. _example-vstring_-vstring_-3: .. dropdown:: vstring_ (np_test_1_all.cpp:8471) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8461 :emphasize-lines: 11 // Copy assignment vstring_ other("Test"); str = other; if (!(str.to_string() == "Test")) { std::string description = std::string("testVStringAssignmentVariableStrings():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(str.to_string() == \"Test\")"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Move assignment str = vstring_("Moved"); if (!(str.to_string() == "Moved")) { std::string description = std::string("testVStringAssignmentVariableStrings():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(str.to_string() == \"Moved\")"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } std::cout << " -> tests passed" << std::endl; } void testVStringOperationsVariableStrings() { std::cout << "========= testVStringOperationsVariableStrings ======================="; .. _example-vstring_-vstring_-4: .. dropdown:: vstring_ (np_test_1_all.cpp:8471) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8461 :emphasize-lines: 11 // Copy assignment vstring_ other("Test"); str = other; if (!(str.to_string() == "Test")) { std::string description = std::string("testVStringAssignmentVariableStrings():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(str.to_string() == \"Test\")"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Move assignment str = vstring_("Moved"); if (!(str.to_string() == "Moved")) { std::string description = std::string("testVStringAssignmentVariableStrings():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(str.to_string() == \"Moved\")"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } std::cout << " -> tests passed" << std::endl; } void testVStringOperationsVariableStrings() { std::cout << "========= testVStringOperationsVariableStrings ======================="; .. _example-vstring_-empty-5: .. dropdown:: empty (np_test_1_all.cpp:6316) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6306 :emphasize-lines: 11 } void test_data_generator_emptyBenchmarkSorting() { std::cout << "========= test_data_generator_empty ======================="; DataGenerator gen(42); // Test empty data generation std::vector data = gen.generate(0, DataPattern::RANDOM); if (!(data.empty())) { std::string description = std::string("test_data_generator_emptyBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(data.empty())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "Empty data generation test passed" << std::endl; std::cout << " -> tests passed" << std::endl; } .. _example-vstring_-at-6: .. dropdown:: at (np_test_1_all.cpp:144) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 134 :emphasize-lines: 11 array.setElementAt({0, 0}, 1); array.setElementAt({0, 1}, 2); array.setElementAt({0, 2}, 3); array.setElementAt({1, 1}, 5); array.setElementAt({2, 2}, 9); // std::cout << "Array after setting elements:" << std::endl; //array.printArray(); // std::cout << "Element at (1,1): " << array.getElementAt({1, 1}) << std::endl; // std::cout << "Element at (2,2): " << array.getElementAt({2, 2}); std::cout << " -> tests passed" << std::endl; } void testArithmetic() { std::cout << "========= testArithmeticOperations ======================="; auto array1 = createFloat32Array({2, 2}, 5.0f); auto array2 = createFloat32Array({2, 2}, 3.0f); .. _example-vstring_-at-7: .. dropdown:: at (np_test_1_all.cpp:144) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 134 :emphasize-lines: 11 array.setElementAt({0, 0}, 1); array.setElementAt({0, 1}, 2); array.setElementAt({0, 2}, 3); array.setElementAt({1, 1}, 5); array.setElementAt({2, 2}, 9); // std::cout << "Array after setting elements:" << std::endl; //array.printArray(); // std::cout << "Element at (1,1): " << array.getElementAt({1, 1}) << std::endl; // std::cout << "Element at (2,2): " << array.getElementAt({2, 2}); std::cout << " -> tests passed" << std::endl; } void testArithmetic() { std::cout << "========= testArithmeticOperations ======================="; auto array1 = createFloat32Array({2, 2}, 5.0f); auto array2 = createFloat32Array({2, 2}, 3.0f); .. _example-vstring_-at_safe-8: .. dropdown:: at_safe (np_test_1_all.cpp:13109) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 13099 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void test_character_access_bounds() { std::cout << "========= test_character_access_bounds ======================="; vstring_ str1("abc"); // Test safe character access within bounds try { char c1 = str1.at_safe(0); // Should work if (!(c1 == 'a')) { std::string description = std::string("test_character_access_bounds():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(c1 == 'a')"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] at_safe(0) on 'abc' returns 'a'\n"; char c2 = str1.at_safe(2); // Should work if (!(c2 == 'c')) { std::string description = std::string("test_character_access_bounds():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(c2 == 'c')"; .. _example-vstring_-resize-9: .. dropdown:: resize (np_test_2_all.cpp:8615) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8605 :emphasize-lines: 11 std::cout << " IPP empty array test: -> [FAIL]"; throw std::runtime_error("IPP empty array test: FAILED - should return empty"); } // std::cout << "[OK] IPP empty array test: PASSED" << std::endl; } // Test 6: Large array performance { const size_t n = 10000; std::vector> keys(2); keys[0].resize(n); keys[1].resize(n); std::mt19937 gen(42); std::uniform_int_distribution dist(0, 1000); for (size_t i = 0; i < n; ++i) { keys[0][i] = dist(gen); keys[1][i] = dist(gen); } .. _example-vstring_-resize-10: .. dropdown:: resize (np_test_2_all.cpp:8615) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8605 :emphasize-lines: 11 std::cout << " IPP empty array test: -> [FAIL]"; throw std::runtime_error("IPP empty array test: FAILED - should return empty"); } // std::cout << "[OK] IPP empty array test: PASSED" << std::endl; } // Test 6: Large array performance { const size_t n = 10000; std::vector> keys(2); keys[0].resize(n); keys[1].resize(n); std::mt19937 gen(42); std::uniform_int_distribution dist(0, 1000); for (size_t i = 0; i < n; ++i) { keys[0][i] = dist(gen); keys[1][i] = dist(gen); } .. _example-vstring_-to_string-11: .. dropdown:: to_string (np_test_1_all.cpp:454) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 444 :emphasize-lines: 11 // Modify through different views view1.setElementAt({0, 0}, 100); view2.setElementAt({2, 1}, 200); // This is (1, 2) in original view3.setElementAt({0, 0}, 300); // This is (1, 1) in original // std::cout << "After modifications through multiple views:" << std::endl; //original.printArray(); // Verify all changes are reflected if (!(original.getElementAt({0, 0}) == 100)) { std::string description = std::string("testAdvancedViewLifecycle():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({0, 0}) == 100)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(original.getElementAt({1, 2}) == 200)) { std::string description = std::string("testAdvancedViewLifecycle():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({1, 2}) == 200)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(original.getElementAt({1, 1}) == 300)) { std::string description = std::string("testAdvancedViewLifecycle():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({1, 1}) == 300)"; .. _example-vstring_-append-12: .. dropdown:: append (np_test_2_all.cpp:7028) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7018 :emphasize-lines: 11 throw std::runtime_error(description); } if (!(deleted_dups.getElementAt({ 2 }) == 3)) { std::string description = std::string("testModificationOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(deleted_dups.getElementAt({ 2 }) == 3)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] Delete with duplicate indices works correctly\n"; // Test append auto appended = append(array, 100); if (!(appended.getSize() == array.getSize() + 1)) { std::string description = std::string("testModificationOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(appended.getSize() == array.getSize() + 1)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(appended.getElementAt({ appended.getSize() - 1 }) == 100)) { std::string description = std::string("testModificationOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(appended.getElementAt({ appended.getSize() - 1 }) == 100)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } .. _example-vstring_-append-13: .. dropdown:: append (np_test_2_all.cpp:7028) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7018 :emphasize-lines: 11 throw std::runtime_error(description); } if (!(deleted_dups.getElementAt({ 2 }) == 3)) { std::string description = std::string("testModificationOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(deleted_dups.getElementAt({ 2 }) == 3)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] Delete with duplicate indices works correctly\n"; // Test append auto appended = append(array, 100); if (!(appended.getSize() == array.getSize() + 1)) { std::string description = std::string("testModificationOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(appended.getSize() == array.getSize() + 1)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(appended.getElementAt({ appended.getSize() - 1 }) == 100)) { std::string description = std::string("testModificationOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(appended.getElementAt({ appended.getSize() - 1 }) == 100)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } .. _example-vstring_-append-14: .. dropdown:: append (np_test_2_all.cpp:7028) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7018 :emphasize-lines: 11 throw std::runtime_error(description); } if (!(deleted_dups.getElementAt({ 2 }) == 3)) { std::string description = std::string("testModificationOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(deleted_dups.getElementAt({ 2 }) == 3)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] Delete with duplicate indices works correctly\n"; // Test append auto appended = append(array, 100); if (!(appended.getSize() == array.getSize() + 1)) { std::string description = std::string("testModificationOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(appended.getSize() == array.getSize() + 1)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(appended.getElementAt({ appended.getSize() - 1 }) == 100)) { std::string description = std::string("testModificationOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(appended.getElementAt({ appended.getSize() - 1 }) == 100)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } .. _example-vstring_-back-15: .. dropdown:: back (np_test_2_all.cpp:8251) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8241 :emphasize-lines: 11 // std::cout << "keys[1] = "; for (size_t i = 0; i < result.size(); ++i) { // std::cout << keys[1][result[i]] << " "; } // std::cout << ""; // Expected order based on primary key (last array), then secondary // Should sort by [9,4,0,4,0,2,1] first, then by [1,5,1,4,3,4,4] bool is_sorted = true; for (size_t i = 1; i < result.size(); ++i) { int prev_primary = keys.back()[result[i - 1]]; int curr_primary = keys.back()[result[i]]; if (prev_primary > curr_primary) { is_sorted = false; break; } else if (prev_primary == curr_primary) { int prev_secondary = keys[0][result[i - 1]]; int curr_secondary = keys[0][result[i]]; if (prev_secondary > curr_secondary) { .. _example-vstring_-back-16: .. dropdown:: back (np_test_2_all.cpp:8251) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8241 :emphasize-lines: 11 // std::cout << "keys[1] = "; for (size_t i = 0; i < result.size(); ++i) { // std::cout << keys[1][result[i]] << " "; } // std::cout << ""; // Expected order based on primary key (last array), then secondary // Should sort by [9,4,0,4,0,2,1] first, then by [1,5,1,4,3,4,4] bool is_sorted = true; for (size_t i = 1; i < result.size(); ++i) { int prev_primary = keys.back()[result[i - 1]]; int curr_primary = keys.back()[result[i]]; if (prev_primary > curr_primary) { is_sorted = false; break; } else if (prev_primary == curr_primary) { int prev_secondary = keys[0][result[i - 1]]; int curr_secondary = keys[0][result[i]]; if (prev_secondary > curr_secondary) { .. _example-vstring_-begin-17: .. dropdown:: begin (np_test_1_all.cpp:6171) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6161 :emphasize-lines: 11 // Test sorted data generation std::vector data = gen.generate(50, DataPattern::SORTED); if (!(data.size() == 50)) { std::string description = std::string("test_data_generator_sortedBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(data.size() == 50)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Verify data is sorted if (!(std::is_sorted(data.begin(), data.end()))) { std::string description = std::string("test_data_generator_sortedBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(std::is_sorted(data.begin(), data.end()))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "Sorted data generation test passed" << std::endl; std::cout << " -> tests passed" << std::endl; } .. _example-vstring_-begin-18: .. dropdown:: begin (np_test_1_all.cpp:6171) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6161 :emphasize-lines: 11 // Test sorted data generation std::vector data = gen.generate(50, DataPattern::SORTED); if (!(data.size() == 50)) { std::string description = std::string("test_data_generator_sortedBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(data.size() == 50)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Verify data is sorted if (!(std::is_sorted(data.begin(), data.end()))) { std::string description = std::string("test_data_generator_sortedBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(std::is_sorted(data.begin(), data.end()))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "Sorted data generation test passed" << std::endl; std::cout << " -> tests passed" << std::endl; } .. _example-vstring_-byte_size-19: .. dropdown:: byte_size (np_test_1_all.cpp:8536) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8526 :emphasize-lines: 11 std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Size operations if (!(str1.size() == 5)) { std::string description = std::string("testVStringOperationsVariableStrings():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(str1.size() == 5)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(str1.byte_size() == 5)) { std::string description = std::string("testVStringOperationsVariableStrings():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(str1.byte_size() == 5)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(!str1.empty())) { std::string description = std::string("testVStringOperationsVariableStrings():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(!str1.empty())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } .. _example-vstring_-c_str-20: .. dropdown:: c_str (np_test_1_all.cpp:13064) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 13054 :emphasize-lines: 11 } void test_fixed_size_string_out_of_bounds() { std::cout << "========= test_fixed_size_string_out_of_bounds ======================="; // Test str_ str_<10> str1("hello"); // length = 5, capacity = 10 // Test substr out-of-bounds auto result1 = str1.substr(20, 5); if (!(std::string(result1.c_str()) == "")) { std::string description = std::string("test_fixed_size_string_out_of_bounds():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(std::string(result1.c_str()) == \"\")"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] str_<10> substr(20, 5) on 'hello' returns empty string\n"; // Test slice out-of-bounds auto result2 = str1.slice(10, 15); if (!(std::string(result2.c_str()) == "")) { std::string description = std::string("test_fixed_size_string_out_of_bounds():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(std::string(result2.c_str()) == \"\")"; .. _example-vstring_-clear-21: .. dropdown:: clear (np_test_2_all.cpp:4161) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4151 :emphasize-lines: 11 auto array = createFloat32Array({ 10, 10 }, static_cast(i)); temp_arrays.push_back(array); auto view = array.view(); temp_views.push_back(view); } // std::cout << "Created 100 arrays and 100 views" << std::endl; // Clear all arrays (views will keep memory alive) temp_arrays.clear(); // std::cout << "Cleared original arrays" << std::endl; // Verify views still work if (!(temp_views[50].getElementAt({ 5, 5 }) == 50.0f)) { std::string description = std::string("testMemoryLeakPrevention():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(temp_views[50].getElementAt({ 5, 5 }) == 50.0f)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] Views still valid after originals cleared"; .. _example-vstring_-data-22: .. dropdown:: data (np_test_1_all.cpp:2084) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2074 :emphasize-lines: 11 } if (!(derived_ptr->extra == 2)) { std::string description = std::string("testObjectHolderEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(derived_ptr->extra == 2)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test 6: Large object handling struct LargeObject { std::vector data; LargeObject() : data(10000, 3.14159) {} // Large object }; LargeObject large; object_ large_obj(large); // Should handle large objects if (!(!large_obj.is_null())) { std::string description = std::string("testObjectHolderEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(!large_obj.is_null())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(large_obj.is_type())) { .. _example-vstring_-end-23: .. dropdown:: end (np_test_1_all.cpp:6171) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6161 :emphasize-lines: 11 // Test sorted data generation std::vector data = gen.generate(50, DataPattern::SORTED); if (!(data.size() == 50)) { std::string description = std::string("test_data_generator_sortedBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(data.size() == 50)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Verify data is sorted if (!(std::is_sorted(data.begin(), data.end()))) { std::string description = std::string("test_data_generator_sortedBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(std::is_sorted(data.begin(), data.end()))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "Sorted data generation test passed" << std::endl; std::cout << " -> tests passed" << std::endl; } .. _example-vstring_-end-24: .. dropdown:: end (np_test_1_all.cpp:6171) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6161 :emphasize-lines: 11 // Test sorted data generation std::vector data = gen.generate(50, DataPattern::SORTED); if (!(data.size() == 50)) { std::string description = std::string("test_data_generator_sortedBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(data.size() == 50)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Verify data is sorted if (!(std::is_sorted(data.begin(), data.end()))) { std::string description = std::string("test_data_generator_sortedBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(std::is_sorted(data.begin(), data.end()))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "Sorted data generation test passed" << std::endl; std::cout << " -> tests passed" << std::endl; } .. _example-vstring_-find-25: .. dropdown:: find (np_test_1_all.cpp:1324) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1314 :emphasize-lines: 11 // Test incompatible shapes auto a = createFloat64Array({2, 3}); auto b = createFloat64Array({4}); // Incompatible for broadcasting bool caught_exception = false; try { auto result = greater(a, b); } catch (const std::invalid_argument& e) { caught_exception = true; std::string msg(e.what()); bool has_msg = msg.find("incompatible shapes") != std::string::npos; if (has_msg != true) { std::cout << "[FAIL] in testComparisonErrorHandling(): exception message missing 'incompatible shapes', got: " << msg << std::endl; throw std::runtime_error("testComparisonErrorHandling(): exception message incorrect"); } } if (caught_exception != true) { std::cout << "[FAIL] in testComparisonErrorHandling(): expected exception not thrown" << std::endl; throw std::runtime_error("testComparisonErrorHandling(): shape incompatibility exception not thrown"); } // std::cout << "[OK] Shape incompatibility exception handling" << std::endl; .. _example-vstring_-find-26: .. dropdown:: find (np_test_1_all.cpp:1324) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1314 :emphasize-lines: 11 // Test incompatible shapes auto a = createFloat64Array({2, 3}); auto b = createFloat64Array({4}); // Incompatible for broadcasting bool caught_exception = false; try { auto result = greater(a, b); } catch (const std::invalid_argument& e) { caught_exception = true; std::string msg(e.what()); bool has_msg = msg.find("incompatible shapes") != std::string::npos; if (has_msg != true) { std::cout << "[FAIL] in testComparisonErrorHandling(): exception message missing 'incompatible shapes', got: " << msg << std::endl; throw std::runtime_error("testComparisonErrorHandling(): exception message incorrect"); } } if (caught_exception != true) { std::cout << "[FAIL] in testComparisonErrorHandling(): expected exception not thrown" << std::endl; throw std::runtime_error("testComparisonErrorHandling(): shape incompatibility exception not thrown"); } // std::cout << "[OK] Shape incompatibility exception handling" << std::endl; .. _example-vstring_-find-27: .. dropdown:: find (np_test_1_all.cpp:1324) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1314 :emphasize-lines: 11 // Test incompatible shapes auto a = createFloat64Array({2, 3}); auto b = createFloat64Array({4}); // Incompatible for broadcasting bool caught_exception = false; try { auto result = greater(a, b); } catch (const std::invalid_argument& e) { caught_exception = true; std::string msg(e.what()); bool has_msg = msg.find("incompatible shapes") != std::string::npos; if (has_msg != true) { std::cout << "[FAIL] in testComparisonErrorHandling(): exception message missing 'incompatible shapes', got: " << msg << std::endl; throw std::runtime_error("testComparisonErrorHandling(): exception message incorrect"); } } if (caught_exception != true) { std::cout << "[FAIL] in testComparisonErrorHandling(): expected exception not thrown" << std::endl; throw std::runtime_error("testComparisonErrorHandling(): shape incompatibility exception not thrown"); } // std::cout << "[OK] Shape incompatibility exception handling" << std::endl; .. _example-vstring_-length-28: .. dropdown:: length (np_test_1_all.cpp:7335) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7325 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void testStringTruncationStringTypes() { std::cout << "========= testStringTruncationStringTypes ======================="; // Test truncation behavior str8 small("This string is way too long for 8 characters"); // std::cout << "Long string in str8: '" << small << "'" << std::endl; // std::cout << "Length: " << small.length() << " (max: " << str8::max_size << ")" << std::endl; str16 medium("This is a medium length string that should be truncated"); // std::cout << "Medium string in str16: '" << medium << "'" << std::endl; // std::cout << "Length: " << medium.length() << " (max: " << str16::max_size << ")" << std::endl; std::cout << " -> tests passed" << std::endl; } void testStringComparisonsStringTypes() { std::cout << "========= testStringComparisonsStringTypes ======================="; .. _example-vstring_-push_back-29: .. dropdown:: push_back (np_test_1_all.cpp:1820) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1810 :emphasize-lines: 11 if (!(*extracted[i].get_as() == values[i])) { std::string description = std::string("testObjectArrayOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(*extracted[i].get_as() == values[i])"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } // Test homogeneous array detection std::vector> double_arrays; for (int i = 0; i < 3; ++i) { double_arrays.push_back(NDArray({2}, i * 1.5)); } NDArray homogeneous = createObjectArrayFromNDArrays(double_arrays); if (!(NestedArray::isHomogeneousArrays(homogeneous))) { std::string description = std::string("testObjectArrayOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(NestedArray::isHomogeneousArrays(homogeneous))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test heterogeneous detection .. _example-vstring_-rbegin-30: .. dropdown:: rbegin (np_test_3_all.cpp:6341) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6331 :emphasize-lines: 11 } void test_large_data_handling() { std::cout << "========= test_large_data_handling ======================="; // Test with data that exceeds typical cache sizes const size_t large_size = 50000; // Should exceed most L3 caches when using ints std::vector large_data(large_size); // Initialize with reverse sorted data for worst case std::iota(large_data.rbegin(), large_data.rend(), 1); std::vector expected = large_data; std::sort(expected.begin(), expected.end()); cache_oblivious_mergesort(large_data.begin(), large_data.end(), std::less()); if (!(large_data == expected)) { std::string description = std::string("test_large_data_handling():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(large_data == expected)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); .. _example-vstring_-rbegin-31: .. dropdown:: rbegin (np_test_3_all.cpp:6341) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6331 :emphasize-lines: 11 } void test_large_data_handling() { std::cout << "========= test_large_data_handling ======================="; // Test with data that exceeds typical cache sizes const size_t large_size = 50000; // Should exceed most L3 caches when using ints std::vector large_data(large_size); // Initialize with reverse sorted data for worst case std::iota(large_data.rbegin(), large_data.rend(), 1); std::vector expected = large_data; std::sort(expected.begin(), expected.end()); cache_oblivious_mergesort(large_data.begin(), large_data.end(), std::less()); if (!(large_data == expected)) { std::string description = std::string("test_large_data_handling():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(large_data == expected)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); .. _example-vstring_-rend-32: .. dropdown:: rend (np_test_3_all.cpp:6341) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6331 :emphasize-lines: 11 } void test_large_data_handling() { std::cout << "========= test_large_data_handling ======================="; // Test with data that exceeds typical cache sizes const size_t large_size = 50000; // Should exceed most L3 caches when using ints std::vector large_data(large_size); // Initialize with reverse sorted data for worst case std::iota(large_data.rbegin(), large_data.rend(), 1); std::vector expected = large_data; std::sort(expected.begin(), expected.end()); cache_oblivious_mergesort(large_data.begin(), large_data.end(), std::less()); if (!(large_data == expected)) { std::string description = std::string("test_large_data_handling():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(large_data == expected)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); .. _example-vstring_-rend-33: .. dropdown:: rend (np_test_3_all.cpp:6341) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6331 :emphasize-lines: 11 } void test_large_data_handling() { std::cout << "========= test_large_data_handling ======================="; // Test with data that exceeds typical cache sizes const size_t large_size = 50000; // Should exceed most L3 caches when using ints std::vector large_data(large_size); // Initialize with reverse sorted data for worst case std::iota(large_data.rbegin(), large_data.rend(), 1); std::vector expected = large_data; std::sort(expected.begin(), expected.end()); cache_oblivious_mergesort(large_data.begin(), large_data.end(), std::less()); if (!(large_data == expected)) { std::string description = std::string("test_large_data_handling():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(large_data == expected)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); .. _example-vstring_-reserve-34: .. dropdown:: reserve (np_test_1_all.cpp:6763) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6753 :emphasize-lines: 11 // arr_2d.print(); std::cout << " -> tests passed" << std::endl; } void testCharArrayPerformanceCharArray() { std::cout << "========= testCharArrayPerformance ======================="; const size_t large_size = 1000; std::vector large_data; large_data.reserve(large_size); for (size_t i = 0; i < large_size; ++i) { large_data.push_back("String_" + std::to_string(i)); } // std::cout << "Creating large CharArray with " << large_size << " strings..." << std::endl; auto large_arr = array<32>(large_data); // std::cout << "Performing upper case conversion..." << std::endl; auto upper_result = upper(large_arr); .. _example-vstring_-rfind-35: .. dropdown:: rfind (np_test_5_all.cpp:1300) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1290 :emphasize-lines: 11 } std::cout << " -> tests passed" << std::endl; } void np_test_string_rfind() { std::cout << "========= rfind search operation ======================="; std::vector strings = { "hello world hello", "test string test" }; auto arr = numpy::char_::array<32>(strings); auto indices = numpy::char_::rfind(arr, "e"); bool passed = (indices.getSize() == arr.size()); if (!passed) { std::cout << " [FAIL] : in np_test_string_rfind() : indices size mismatch"; throw std::runtime_error("np_test_string_rfind failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-vstring_-size-36: .. dropdown:: size (np_test_1_all.cpp:47) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 37 :emphasize-lines: 11 using namespace numpy; using namespace numpy::benchmark; using namespace numpy::char_; // Helper functions for array comparison tests namespace { const double TOLERANCE = 1e-10; bool allTrue(const NDArray& arr) { std::vector indices(arr.getShape().size(), 0); do { if (!arr.getElementAt(indices)) { return false; } } while (incrementIndices(indices, arr.getShape())); return true; } bool allFalse(const NDArray& arr) { std::vector indices(arr.getShape().size(), 0); .. _example-vstring_-slice-37: .. dropdown:: slice (np_test_1_all.cpp:13009) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 12999 :emphasize-lines: 11 // Test substr valid start but excessive length auto result3 = str1.substr(2, 10); // start=2, len=10 on string of length 5 if (!(result3.to_string() == "llo")) { std::string description = std::string("test_variable_string_out_of_bounds():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result3.to_string() == \"llo\")"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] vstring_ substr(2, 10) on 'hello' returns 'llo'\n"; // Test slice method with out-of-bounds auto result4 = str1.slice(10, 15); // slice[10:15] on string of length 5 if (!(result4.to_string() == "")) { std::string description = std::string("test_variable_string_out_of_bounds():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result4.to_string() == \"\")"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] vstring_ slice(10, 15) on 'hello' returns empty string\n"; // Test slice with negative indices auto result5 = str1.slice(-2, -1); // slice[-2:-1] should be "l" if (!(result5.to_string() == "l")) { .. _example-vstring_-substr-38: .. dropdown:: substr (np_test_1_all.cpp:7373) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7363 :emphasize-lines: 11 str32 s2(" World"); str32 s3 = s1 + s2; // std::cout << "'" << s1 << "' + '" << s2 << "' = '" << s3 << "'" << std::endl; s1 += s2; // std::cout << "After +=: '" << s1 << "'" << std::endl; // Test substring str32 full("Programming"); str32 sub = full.substr(0, 7); // std::cout << "Substring of '" << full << "' (0,7): '" << sub << "'" << std::endl; // Test find str32 search("Hello World Programming"); size_t pos = search.find(str32("World")); // std::cout << "Position of 'World' in '" << search << "': " << pos << std::endl; // Test utility functions str32 lower = to_lower(str32("UPPERCASE")); str32 upper = to_upper(str32("lowercase"));