TimedeltaIndex#
-
class numpy::TimedeltaIndex#
numpy C++ class.
Example#
#include <numpy/np_ndarray.h>
using namespace numpy;
// Use TimedeltaIndex
TimedeltaIndex obj;
// ... operations ...
Constructors#
Signature |
Location |
Example |
|---|---|---|
|
df_timedelta_index.h:111 |
|
|
df_timedelta_index.h:121 |
|
|
df_timedelta_index.h:131 |
|
|
df_timedelta_index.h:149 |
|
|
df_timedelta_index.h:160 |
|
|
df_timedelta_index.h:167 |
Indexing / Selection#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
numpy::NDArray<numpy::int64> |
df_timedelta_index.h:1382 |
|
|
numpy::NDArray<numpy::int64> |
df_timedelta_index.h:1426 |
|
|
df_timedelta_index.h:1459 |
||
|
TimedeltaIndex |
df_timedelta_index.h:1521 |
|
|
int64_t |
df_timedelta_index.h:1534 |
|
|
size_t |
df_timedelta_index.h:1557 |
|
|
numpy::timedelta64 |
df_timedelta_index.h:1879 |
|
|
TimedeltaIndex |
df_timedelta_index.h:2043 |
|
|
size_t |
df_timedelta_index.h:2168 |
|
|
TimedeltaIndex |
df_timedelta_index.h:2377 |
|
|
TimedeltaIndex |
df_timedelta_index.h:2736 |
|
|
TimedeltaIndex |
df_timedelta_index.h:2761 |
Shape Manipulation#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
TimedeltaIndex |
df_timedelta_index.h:2612 |
|
|
std::vector<std::optional<numpy::timedelta64>> |
df_timedelta_index.h:2071 |
|
|
TimedeltaIndex |
df_timedelta_index.h:2605 |
Statistics#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
std::optional<numpy::timedelta64> |
df_timedelta_index.h:462 |
|
|
std::optional<numpy::timedelta64> |
df_timedelta_index.h:1956 |
|
|
std::optional<numpy::timedelta64> |
df_timedelta_index.h:431 |
|
|
IntegerArray<numpy::int64> |
df_timedelta_index.h:243 |
|
|
std::optional<numpy::timedelta64> |
df_timedelta_index.h:577 |
|
|
std::optional<numpy::timedelta64> |
df_timedelta_index.h:493 |
Sorting#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
numpy::NDArray<numpy::int64> |
df_timedelta_index.h:809 |
|
|
TimedeltaIndex |
df_timedelta_index.h:2299 |
|
|
TimedeltaIndex |
df_timedelta_index.h:2309 |
|
|
df_timedelta_index.h:2328 |
Math Operations#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
TimedeltaIndex |
df_timedelta_index.h:395 |
|
|
TimedeltaIndex |
df_timedelta_index.h:385 |
|
|
TimedeltaIndex |
df_timedelta_index.h:375 |
Comparison#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
bool |
df_timedelta_index.h:1309 |
Logical#
Set Operations#
I/O#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
TimedeltaIndex |
df_timedelta_index.h:2400 |
|
|
FrameData |
df_timedelta_index.h:2514 |
|
|
std::vector<int64_t> |
df_timedelta_index.h:2414 |
|
|
std::vector<std::optional<numpy::timedelta64>> |
df_timedelta_index.h:2437 |
|
|
numpy::NDArray<numpy::timedelta64> |
df_timedelta_index.h:2456 |
|
|
std::vector<std::optional<PyTimedelta>> |
df_timedelta_index.h:362 |
|
|
SeriesData |
df_timedelta_index.h:2573 |
|
|
std::vector<std::optional<numpy::timedelta64>> |
df_timedelta_index.h:2468 |
|
|
std::string |
df_timedelta_index.h:655 |
|
|
std::vector<std::optional<numpy::timedelta64>> |
df_timedelta_index.h:2444 |
Joining / Splitting#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
TimedeltaIndex |
df_timedelta_index.h:780 |
|
|
TimedeltaIndex |
df_timedelta_index.h:789 |
|
|
TimedeltaIndex |
df_timedelta_index.h:992 |
|
|
TimedeltaIndex |
df_timedelta_index.h:1011 |
|
|
TimedeltaIndex |
df_timedelta_index.h:1655 |
|
|
TimedeltaIndex |
df_timedelta_index.h:2120 |
|
|
TimedeltaIndex |
df_timedelta_index.h:2138 |
Type Handling#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
AsTypeResult |
df_timedelta_index.h:921 |
|
|
numpy::NDArray<numpy::int64> |
df_timedelta_index.h:871 |
|
|
TimedeltaIndex |
df_timedelta_index.h:631 |
|
|
TimedeltaIndex |
df_timedelta_index.h:2722 |
Type Checking#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
bool |
df_timedelta_index.h:1730 |
|
|
bool |
df_timedelta_index.h:1742 |
|
|
bool |
df_timedelta_index.h:1750 |
|
|
bool |
df_timedelta_index.h:1758 |
|
|
bool |
df_timedelta_index.h:1766 |
|
|
bool |
df_timedelta_index.h:1774 |
|
|
bool |
df_timedelta_index.h:1782 |
|
|
bool |
df_timedelta_index.h:1790 |
Other Methods#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
int64_t |
df_timedelta_index.h:552 |
|
|
int64_t |
df_timedelta_index.h:528 |
|
|
TimedeltaArray |
df_timedelta_index.h:140 |
|
|
TimedeltaArray |
df_timedelta_index.h:153 |
|
|
TimedeltaIndex |
df_timedelta_index.h:405 |
|
|
int64_t |
df_timedelta_index.h:829 |
|
|
std::vector<int64_t> |
df_timedelta_index.h:854 |
|
|
std::unique_ptr<IndexBase> |
df_timedelta_index.h:622 |
|
|
TimedeltaComponents |
df_timedelta_index.h:251 |
|
|
static TimedeltaIndex |
df_timedelta_index.h:760 |
|
|
IntegerArray<numpy::int64> |
df_timedelta_index.h:219 |
|
|
TimedeltaIndex |
df_timedelta_index.h:1031 |
|
|
TimedeltaIndex |
df_timedelta_index.h:1108 |
|
|
TimedeltaIndex |
df_timedelta_index.h:1079 |
|
|
TimedeltaIndex |
df_timedelta_index.h:1124 |
|
|
TimedeltaIndex |
df_timedelta_index.h:1151 |
|
|
TimedeltaIndex |
df_timedelta_index.h:1222 |
|
|
TimedeltaIndex |
df_timedelta_index.h:1234 |
|
|
std::string |
df_timedelta_index.h:207 |
|
|
BooleanArray |
df_timedelta_index.h:1248 |
|
|
std::pair<numpy::NDArray<numpy::int64>, TimedeltaIndex> |
df_timedelta_index.h:1336 |
|
|
TimedeltaIndex |
df_timedelta_index.h:1350 |
|
|
numpy::NDArray<double> |
df_timedelta_index.h:949 |
|
|
std::vector<std::string> |
df_timedelta_index.h:1364 |
|
|
std::unordered_map<int64_t, std::vector<size_t>> |
df_timedelta_index.h:1593 |
|
|
bool |
df_timedelta_index.h:1616 |
|
|
bool |
df_timedelta_index.h:1629 |
|
|
* TimedeltaIndex |
df_timedelta_index.h:916 |
|
|
* TimedeltaIndex |
df_timedelta_index.h:2503 |
|
|
* TimedeltaIndex |
df_timedelta_index.h:2566 |
|
|
numpy::NDArray<numpy::int64> |
df_timedelta_index.h:1499 |
|
|
TimedeltaIndex |
df_timedelta_index.h:1641 |
|
|
std::string |
df_timedelta_index.h:199 |
|
|
numpy::NDArray<numpy::int64> |
df_timedelta_index.h:937 |
|
|
numpy::NDArray<numpy::int64> |
df_timedelta_index.h:813 |
|
|
TimedeltaIndex |
df_timedelta_index.h:1714 |
|
|
TimedeltaIndex |
df_timedelta_index.h:1685 |
|
|
BooleanArray |
df_timedelta_index.h:1833 |
|
|
BooleanArray |
df_timedelta_index.h:1846 |
|
|
TimedeltaIndex |
df_timedelta_index.h:1904 |
|
|
TimedeltaIndex |
df_timedelta_index.h:1926 |
|
|
size_t |
df_timedelta_index.h:1997 |
|
|
IntegerArray<numpy::int64> |
df_timedelta_index.h:235 |
|
|
numpy::NDArray<numpy::int64> |
df_timedelta_index.h:1504 |
|
|
BooleanArray |
df_timedelta_index.h:1854 |
|
|
BooleanArray |
df_timedelta_index.h:1867 |
|
|
size_t |
df_timedelta_index.h:2010 |
|
|
df_timedelta_index.h:2092 |
||
|
TimedeltaIndex |
df_timedelta_index.h:643 |
|
|
std::string |
df_timedelta_index.h:696 |
|
|
std::vector<std::optional<bool>> |
df_timedelta_index.h:1250 |
|
|
IntegerArray<numpy::int64> |
df_timedelta_index.h:227 |
|
|
TimedeltaIndex |
df_timedelta_index.h:2212 |
|
|
TimedeltaIndex |
df_timedelta_index.h:2220 |
|
|
TimedeltaIndex |
df_timedelta_index.h:416 |
|
|
TimedeltaIndex |
df_timedelta_index.h:2235 |
|
|
std::vector<size_t> |
df_timedelta_index.h:2276 |
|
|
std::pair<size_t, size_t> |
df_timedelta_index.h:2252 |
|
|
TimedeltaIndex |
df_timedelta_index.h:2358 |
|
|
TimedeltaIndex |
df_timedelta_index.h:2626 |
|
|
TimedeltaIndex |
df_timedelta_index.h:2657 |
|
|
df_timedelta_index.h:2687 |
Code Examples#
The following examples are extracted from the test suite.
item (np_test_4_all.cpp:20048)
20038/**
20039 * @file np_test_phase6.cpp
20040 * @brief Test suite for Phase 6A - HIGH PRIORITY NDArray methods and masked array utilities
20041 *
20042 * Tests:
20043 * - ndarray.nonzero() - Return indices of non-zero elements
20044 * - ndarray.tobytes() - Convert to bytes representation
20045 * - ndarray.tolist() - Convert to nested list string
20046 * - ndarray.fill() - Fill with scalar value (in-place)
20047 * - ndarray.item() - Get single element as scalar
20048 * - ndarray.itemset() - Set single element value
20049 * - ma.masked_all_like() - Create masked array with all elements masked
20050 *
20051 * Created: 2025-10-28
20052 * Status: Phase 6A Implementation - HIGH PRIORITY functions
20053 */
20054
20055#include "../numpy/np_ndarray.h"
20056#include "../numpy/np_masked_array.h"
20057#include <iostream>
putmask (np_test_5_all.cpp:4659)
4649 mask.setElementAt({ 1 }, false);
4650 mask.setElementAt({ 2 }, true);
4651 mask.setElementAt({ 3 }, false);
4652 mask.setElementAt({ 4 }, true);
4653
4654 // Create value array
4655 numpy::NDArray<int32_t> values({ 1 });
4656 values.setElementAt({ 0 }, 99);
4657
4658 // Apply putmask
4659 numpy::putmask(arr, mask, values);
4660
4661 // Expected: [99, 2, 99, 4, 99]
4662 if (arr.getElementAt({ 0 }) != 99 || arr.getElementAt({ 1 }) != 2 ||
4663 arr.getElementAt({ 2 }) != 99 || arr.getElementAt({ 3 }) != 4 ||
4664 arr.getElementAt({ 4 }) != 99) {
4665 std::cout << " [FAIL] : in np_test_putmask() : Incorrect result";
4666 throw std::runtime_error("Test failed");
4667 }
4668
4669 // Test with multiple values
searchsorted (np_test_2_all.cpp:8204)
8194 // Test missing NumPy functions
8195 void test_numpy_functions() {
8196 std::cout << "========= NumPy Functions Test ==========================";
8197
8198 // Test searchsorted
8199 {
8200 std::vector<int> sorted_arr = { 1, 3, 5, 7, 9 };
8201 std::vector<int> values = { 2, 6, 8 };
8202
8203 auto result = numpy::searchsorted(sorted_arr, values, "left");
8204 std::vector<size_t> expected = { 1, 3, 4 };
8205
8206 if (result != expected) {
8207 std::cout << " searchsorted (left): -> [FAIL]";
8208 throw std::runtime_error("searchsorted (left): FAILED - incorrect result indices");
8209 }
8210 // std::cout << "[OK] searchsorted (left): PASSED" << std::endl;
8211
8212 auto result_right = numpy::searchsorted(sorted_arr, values, "right");
8213 std::vector<size_t> expected_right = { 1, 3, 4 };
take (np_test_5_all.cpp:4313)
4303 for (size_t i = 0; i < 6; ++i) {
4304 arr.setElementAt({ i }, static_cast<int32_t>(i * 10)); // [0, 10, 20, 30, 40, 50]
4305 }
4306
4307 // Take specific indices
4308 numpy::NDArray<size_t> indices({ 3 });
4309 indices.setElementAt({ 0 }, 5); // 50
4310 indices.setElementAt({ 1 }, 2); // 20
4311 indices.setElementAt({ 2 }, 0); // 0
4312
4313 auto result = numpy::take(arr, indices);
4314
4315 if (result.getSize() != 3) {
4316 std::cout << " [FAIL] : in np_test_take_basic() : Wrong result size";
4317 throw std::runtime_error("Test failed");
4318 }
4319
4320 if (result.getElementAt({ 0 }) != 50 || result.getElementAt({ 1 }) != 20 || result.getElementAt({ 2 }) != 0) {
4321 std::cout << " [FAIL] : in np_test_take_basic() : Wrong values extracted";
4322 throw std::runtime_error("Test failed");
4323 }
where (np_test_3_all.cpp:15253)
15243 auto y_data = createInt32Array({ 4 }, 0);
15244 y_data.setElementAt({ 0 }, 100);
15245 y_data.setElementAt({ 1 }, 200);
15246 y_data.setElementAt({ 2 }, 300);
15247 y_data.setElementAt({ 3 }, 400);
15248
15249 auto y_mask = createBoolArray({ 4 }, false);
15250 MaskedArray<int32> y(y_data, y_mask, -999);
15251
15252 // Test where() selection
15253 auto result = where(condition, x, y);
15254
15255 if (!(result.getElementAt({ 0 }) == 10)) {
15256 std::string description = std::string("test_where_selection():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result.getElementAt({ 0 }) == 10)";
15257 std::cout << std::string("[FAIL] ") + description << std::endl;
15258 throw std::runtime_error(description);
15259 }
15260 if (!(result.getElementAt({ 1 }) == 200)) {
15261 std::string description = std::string("test_where_selection():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result.getElementAt({ 1 }) == 200)";
15262 std::cout << std::string("[FAIL] ") + description << std::endl;
where (np_test_3_all.cpp:15253)
15243 auto y_data = createInt32Array({ 4 }, 0);
15244 y_data.setElementAt({ 0 }, 100);
15245 y_data.setElementAt({ 1 }, 200);
15246 y_data.setElementAt({ 2 }, 300);
15247 y_data.setElementAt({ 3 }, 400);
15248
15249 auto y_mask = createBoolArray({ 4 }, false);
15250 MaskedArray<int32> y(y_data, y_mask, -999);
15251
15252 // Test where() selection
15253 auto result = where(condition, x, y);
15254
15255 if (!(result.getElementAt({ 0 }) == 10)) {
15256 std::string description = std::string("test_where_selection():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result.getElementAt({ 0 }) == 10)";
15257 std::cout << std::string("[FAIL] ") + description << std::endl;
15258 throw std::runtime_error(description);
15259 }
15260 if (!(result.getElementAt({ 1 }) == 200)) {
15261 std::string description = std::string("test_where_selection():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result.getElementAt({ 1 }) == 200)";
15262 std::cout << std::string("[FAIL] ") + description << std::endl;
T (np_test_1_all.cpp:20062)
20052 std::mt19937 gen;
20053
20054 public:
20055 MatrixGenerator(unsigned seed = 12345) : gen(seed) {}
20056
20057 // Generate real matrix with known real eigenvalues
20058 NDArray<T> real_matrix_real_eigenvalues(size_t n) {
20059 // Create diagonal matrix with known eigenvalues
20060 NDArray<T> D = NDArray<T>::createZeros({ n, n });
20061 for (size_t i = 0; i < n; ++i) {
20062 D.setElementAt({ i, i }, T(i + 1)); // Eigenvalues 1, 2, 3, ...
20063 }
20064
20065 // Create random orthogonal transformation
20066 auto Q = random_orthogonal_matrix(n);
20067 auto QT = Q.transposeArray();
20068
20069 // A = Q * D * Q^T
20070 return matmul(matmul(Q, D), QT);
20071 }
ravel (np_test_1_all.cpp:4082)
4072 std::cout << "Testing ravel function...\n";
4073
4074 // Test with 2-D array
4075 auto arr_2d = createFloat64Array({ 2, 3 });
4076 for (size_t i = 0; i < 2; ++i) {
4077 for (size_t j = 0; j < 3; ++j) {
4078 arr_2d.setElementAt({ i, j }, static_cast<double>(i * 3 + j));
4079 }
4080 }
4081
4082 auto raveled = ravel(arr_2d);
4083 if (!(raveled.getShape() == std::vector<size_t>({ 6 }))) {
4084 std::string description = std::string("testRavelFunction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(raveled.getShape() == std::vector<size_t>({ 6 }))";
4085 std::cout << std::string("[FAIL] ") + description << std::endl;
4086 throw std::runtime_error(description);
4087 }
4088
4089 for (size_t i = 0; i < 6; ++i) {
4090 if (!(isApproxEqualExt(raveled.getElementAt({ i }), static_cast<double>(i)))) {
4091 std::string description = std::string("testRavelFunction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(isApproxEqualExt(raveled.getElementAt({ i }), static_cast<double>(i)))";
4092 std::cout << std::string("[FAIL] ") + description << std::endl;
transpose (np_test_2_all.cpp:4973)
4963 }
4964
4965 /**
4966 * Test matrix properties and methods
4967 */
4968 void testMatrixProperties() {
4969 std::cout << "========= testMatrixProperties =======================";
4970
4971 // Test transpose
4972 numpy::Matrix<double> m("1 2 3; 4 5 6");
4973 auto mt = m.transpose();
4974 assert_test(mt.rows() == 3, "Transpose rows");
4975 assert_test(mt.cols() == 2, "Transpose cols");
4976 assert_test(std::abs(mt(0, 1) - 4.0) < 1e-10, "Transpose element");
4977 assert_test(std::abs(mt(2, 0) - 3.0) < 1e-10, "Transpose element");
4978
4979 // Test trace for square matrix
4980 numpy::Matrix<double> square("1 2; 3 4");
4981 double tr = square.trace();
4982 assert_test(std::abs(tr - 5.0) < 1e-10, "Matrix trace");
max (np_test_1_all.cpp:7274)
7264 if (sizeof(uintp) == sizeof(void*)) {
7265 // std::cout << " -> uintp size matches pointer size";
7266 } else {
7267 // std::cout << " ✗ uintp size doesn't match pointer size" << std::endl;
7268 }
7269
7270 // Test range limits
7271 // std::cout << "Range Information:" << std::endl;
7272 // std::cout << " intp min: " << std::numeric_limits<intp>::min() << std::endl;
7273 // std::cout << " intp max: " << std::numeric_limits<intp>::max() << std::endl;
7274 // std::cout << " uintp max: " << std::numeric_limits<uintp>::max() << std::endl;
7275 // std::cout << " longdouble digits: " << std::numeric_limits<longdouble>::digits << std::endl;
7276
7277 std::cout << " -> tests passed" << std::endl;
7278}
7279
7280void testComplexArithmeticExtendedTypes() {
7281 std::cout << "========= testComplexArithmeticExtendedTypes =======================";
7282
7283 clongdouble c1(3.0L, 4.0L); // 3 + 4i
median (np_test_1_all.cpp:11882)
11872 std::cout << "========= testMedianAndPercentiles =======================";
11873
11874 // Test median with odd number of elements
11875 auto odd_array = createInt32Array({ 5 }, 0);
11876 odd_array.setElementAt({ 0 }, 1);
11877 odd_array.setElementAt({ 1 }, 3);
11878 odd_array.setElementAt({ 2 }, 5);
11879 odd_array.setElementAt({ 3 }, 7);
11880 odd_array.setElementAt({ 4 }, 9);
11881
11882 auto median_odd = median(odd_array);
11883 if (!(approx_equal(median_odd.getElementAt({ 0 }), 5.0, 1e-10))) {
11884 std::string description = std::string("testMedianAndPercentiles():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(median_odd.getElementAt({ 0 }), 5.0, 1e-10))";
11885 std::cout << std::string("[FAIL] ") + description << std::endl;
11886 throw std::runtime_error(description);
11887 }
11888 // std::cout << "[OK] Median with odd count works correctly\n";
11889
11890 // Test median with even number of elements
11891 auto even_array = createInt32Array({ 4 }, 0);
11892 even_array.setElementAt({ 0 }, 1);
min (np_test_1_all.cpp:2350)
2340 if (i % 3 == 0) {
2341 large_array.setElementAt({i}, object_(static_cast<int>(i)));
2342 } else if (i % 3 == 1) {
2343 large_array.setElementAt({i}, object_(static_cast<double>(i) * 0.5));
2344 } else {
2345 large_array.setElementAt({i}, object_(std::string("str") + std::to_string(i)));
2346 }
2347 }
2348
2349 // Verify pattern
2350 for (size_t i = 0; i < std::min(large_size, size_t(100)); ++i) { // Check first 100
2351 object_ obj = large_array.getElementAt({i});
2352 if (i % 3 == 0) {
2353 if (!(obj.is_type<int>())) {
2354 std::string description = std::string("testArrayEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(obj.is_type<int>())";
2355 std::cout << std::string("[FAIL] ") + description << std::endl;
2356 throw std::runtime_error(description);
2357 }
2358 } else if (i % 3 == 1) {
2359 if (!(obj.is_type<double>())) {
2360 std::string description = std::string("unknown_function():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(obj.is_type<double>())";
nanoseconds (np_test_5_all.cpp:21255)
21245 errors++;
21246 }
21247 }
21248
21249 if (errors == 0) {
21250 std::cout << "np_test_timedelta_constructors -> tests passed" << std::endl;
21251 }
21252 return errors;
21253}
21254
21255// =============================================================================
21256// Test 2: Static Factory Methods
21257// =============================================================================
21258int np_test_timedelta_static_factories() {
21259 int errors = 0;
21260
21261 // NaT()
21262 {
21263 numpy::Timedelta nat = numpy::Timedelta::NaT();
21264 if (!nat.isNaT()) {
21265 std::cout << "[FAIL] np_test_timedelta_static_factories: NaT().isNaT() should be true"
std (np_test_1_all.cpp:11836)
11826 auto var_sample = var(array, std::nullopt, 1); // ddof=1 for sample variance
11827 double expected_sample_var = 1.25 * 4.0 / 3.0; // Bessel's correction
11828 if (!(approx_equal(var_sample.getElementAt({ 0 }), expected_sample_var, 1e-10))) {
11829 std::string description = std::string("testVarianceAndStandardDeviation():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(var_sample.getElementAt({ 0 }), expected_sample_var, 1e-10))";
11830 std::cout << std::string("[FAIL] ") + description << std::endl;
11831 throw std::runtime_error(description);
11832 }
11833 // std::cout << "[OK] Sample variance works correctly\n";
11834
11835 // Test standard deviation
11836 auto std_result = numpy::std(array, std::nullopt, 0);
11837 if (!(approx_equal(std_result.getElementAt({ 0 }), std::sqrt(expected_var), 1e-10))) {
11838 std::string description = std::string("testVarianceAndStandardDeviation():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(std_result.getElementAt({ 0 }), std::sqrt(expected_var), 1e-10))";
11839 std::cout << std::string("[FAIL] ") + description << std::endl;
11840 throw std::runtime_error(description);
11841 }
11842 // std::cout << "[OK] Standard deviation works correctly\n";
11843
11844 // Test along axis
11845 auto array2d = createFloat64Array({ 2, 2 }, 0);
11846 array2d.setElementAt({ 0, 0 }, 1.0);
sum (np_test_1_all.cpp:11766)
11756 throw std::runtime_error(description);
11757 }
11758 if (!(approx_equal(mean_axis1.getElementAt({ 1 }), 5.0, 1e-10))) {
11759 std::string description = std::string("testBasicStatistics():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(mean_axis1.getElementAt({ 1 }), 5.0, 1e-10))";
11760 std::cout << std::string("[FAIL] ") + description << std::endl;
11761 throw std::runtime_error(description);
11762 }
11763 // std::cout << "[OK] mean along axis 1 works correctly\n";
11764
11765 // Test sum
11766 auto sum_all = sum(array);
11767 if (!(sum_all.getElementAt({ 0 }) == 21)) {
11768 std::string description = std::string("testBasicStatistics():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(sum_all.getElementAt({ 0 }) == 21)";
11769 std::cout << std::string("[FAIL] ") + description << std::endl;
11770 throw std::runtime_error(description);
11771 }
11772 // std::cout << "[OK] sum works correctly\n";
11773
11774 // Test min and max
11775 auto min_all = min(array);
11776 auto max_all = max(array);
argsort (np_test_1_all.cpp:16841)
16831 std::cout << "========= test_argsort =======================";
16832
16833 // Test 1D array argsort
16834 NDArray<int> arr1d({ 5 });
16835 arr1d.setElementAt({ 0 }, 30); // index 0
16836 arr1d.setElementAt({ 1 }, 10); // index 1
16837 arr1d.setElementAt({ 2 }, 40); // index 2
16838 arr1d.setElementAt({ 3 }, 20); // index 3
16839 arr1d.setElementAt({ 4 }, 50); // index 4
16840
16841 auto indices = argsort(arr1d);
16842
16843 // Expected order: 10(idx1), 20(idx3), 30(idx0), 40(idx2), 50(idx4)
16844 if (!(indices.getElementAt({ 0 }) == 1)) {
16845 std::string description = std::string("test_argsort():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(indices.getElementAt({ 0 }) == 1)";
16846 std::cout << std::string("[FAIL] ") + description << std::endl;
16847 throw std::runtime_error(description);
16848 }
16849 if (!(indices.getElementAt({ 1 }) == 3)) {
16850 std::string description = std::string("test_argsort():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(indices.getElementAt({ 1 }) == 3)";
16851 std::cout << std::string("[FAIL] ") + description << std::endl;
sort (np_test_1_all.cpp:6258)
6248 std::vector<int> data = gen.generate(100, DataPattern::FEW_UNIQUE);
6249
6250 if (!(data.size() == 100)) {
6251 std::string description = std::string("test_data_generator_few_uniqueBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(data.size() == 100)";
6252 std::cout << std::string("[FAIL] ") + description << std::endl;
6253 throw std::runtime_error(description);
6254 }
6255
6256 // Count unique values
6257 std::vector<int> sorted_copy = data;
6258 std::sort(sorted_copy.begin(), sorted_copy.end());
6259 auto unique_end = std::unique(sorted_copy.begin(), sorted_copy.end());
6260 size_t unique_count = std::distance(sorted_copy.begin(), unique_end);
6261
6262 // Should have significantly fewer unique values than total elements
6263 if (!(unique_count < data.size() / 2)) {
6264 std::string description = std::string("test_data_generator_few_uniqueBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(unique_count < data.size() / 2)";
6265 std::cout << std::string("[FAIL] ") + description << std::endl;
6266 throw std::runtime_error(description);
6267 }
ceil (np_test_2_all.cpp:5735)
5725 throw std::runtime_error(description);
5726 }
5727 if (!(approx_equal(floor_result.getElementAt({ 5 }), std::floor(2.7)))) {
5728 std::string description = std::string("testRoundingFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(floor_result.getElementAt({ 5 }), std::floor(2.7)))";
5729 std::cout << std::string("[FAIL] ") + description << std::endl;
5730 throw std::runtime_error(description);
5731 }
5732 // std::cout << "[OK] floor function works correctly\n";
5733
5734 // Test ceil
5735 auto ceil_result = ceil(values);
5736 if (!(approx_equal(ceil_result.getElementAt({ 0 }), std::ceil(-2.7)))) {
5737 std::string description = std::string("testRoundingFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(ceil_result.getElementAt({ 0 }), std::ceil(-2.7)))";
5738 std::cout << std::string("[FAIL] ") + description << std::endl;
5739 throw std::runtime_error(description);
5740 }
5741 if (!(approx_equal(ceil_result.getElementAt({ 5 }), std::ceil(2.7)))) {
5742 std::string description = std::string("testRoundingFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(ceil_result.getElementAt({ 5 }), std::ceil(2.7)))";
5743 std::cout << std::string("[FAIL] ") + description << std::endl;
5744 throw std::runtime_error(description);
5745 }
floor (np_test_2_all.cpp:5721)
5711 throw std::runtime_error(description);
5712 }
5713 if (!(approx_equal(round_result.getElementAt({ 5 }), std::round(2.7)))) {
5714 std::string description = std::string("testRoundingFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(round_result.getElementAt({ 5 }), std::round(2.7)))";
5715 std::cout << std::string("[FAIL] ") + description << std::endl;
5716 throw std::runtime_error(description);
5717 }
5718 // std::cout << "[OK] round function works correctly\n";
5719
5720 // Test floor
5721 auto floor_result = floor(values);
5722 if (!(approx_equal(floor_result.getElementAt({ 0 }), std::floor(-2.7)))) {
5723 std::string description = std::string("testRoundingFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(floor_result.getElementAt({ 0 }), std::floor(-2.7)))";
5724 std::cout << std::string("[FAIL] ") + description << std::endl;
5725 throw std::runtime_error(description);
5726 }
5727 if (!(approx_equal(floor_result.getElementAt({ 5 }), std::floor(2.7)))) {
5728 std::string description = std::string("testRoundingFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(floor_result.getElementAt({ 5 }), std::floor(2.7)))";
5729 std::cout << std::string("[FAIL] ") + description << std::endl;
5730 throw std::runtime_error(description);
5731 }
round (np_test_1_all.cpp:23769)
23759 throw std::runtime_error(description);
23760 }
23761 if (!(power_scalar_result.getShape() == array.getShape())) {
23762 std::string description = std::string("testMathFunctionSignatures():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(power_scalar_result.getShape() == array.getShape())";
23763 std::cout << std::string("[FAIL] ") + description << std::endl;
23764 throw std::runtime_error(description);
23765 }
23766 // std::cout << "[OK] Power functions have correct signatures\n";
23767
23768 // Test rounding functions with new decimals parameter
23769 auto round_result = round(array); // Default decimals=0
23770 auto round_decimals_result = round(array, 2); // With decimals
23771 if (!(round_result.getShape() == array.getShape())) {
23772 std::string description = std::string("testMathFunctionSignatures():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(round_result.getShape() == array.getShape())";
23773 std::cout << std::string("[FAIL] ") + description << std::endl;
23774 throw std::runtime_error(description);
23775 }
23776 if (!(round_decimals_result.getShape() == array.getShape())) {
23777 std::string description = std::string("testMathFunctionSignatures():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(round_decimals_result.getShape() == array.getShape())";
23778 std::cout << std::string("[FAIL] ") + description << std::endl;
23779 throw std::runtime_error(description);
all (np_test_4_all.cpp:23928)
23918 }
23919 }
23920 }
23921
23922 auto result = numpy::einsum<numpy::float64>("ijk->ik", {A});
23923
23924 if (result.getShape()[0] != 2 || result.getShape()[1] != 2) {
23925 throw std::runtime_error("einsum partial sum shape wrong");
23926 }
23927
23928 // Sum over j: 1+2+3 = 6 for all (i,k) positions
23929 if (std::abs(result.getElementAt({0, 0}) - 6.0) > 1e-10 ||
23930 std::abs(result.getElementAt({1, 1}) - 6.0) > 1e-10) {
23931 throw std::runtime_error("einsum partial sum values wrong");
23932 }
23933
23934 // std::cout << " OK: Partial sum: 'ijk->ik'\n";
23935 }
23936
23937 // Test 5: Size-1 dimension handling
23938 {
any (np_test_2_all.cpp:16758)
16748 // ANY() TESTS - SCALAR RESULT
16749 // ============================================================================
16750
16751 void np_test_logic_any_scalar_all_false() {
16752 std::cout << "========= any: all false elements =======================";
16753
16754 // Create array with all false/zero elements
16755 std::vector<double> data = { 0.0, 0.0, 0.0 };
16756 numpy::NDArray<double> arr = numpy::createArrayFromVector<double>({ 3 }, data);
16757
16758 bool result = numpy::any(arr);
16759
16760 if (result != false) {
16761 std::cout << " [FAIL] : in np_test_logic_any_scalar_all_false() : expected false for all-zero array";
16762 throw std::runtime_error("np_test_logic_any_scalar_all_false failed: expected false");
16763 }
16764
16765 std::cout << " -> tests passed" << std::endl;
16766 }
16767
16768 void np_test_logic_any_scalar_all_true() {
isin (np_test_1_all.cpp:19434)
19424 std::cout << std::string("[FAIL] ") + description << std::endl;
19425 throw std::runtime_error(description);
19426 }
19427 if (!(membership.getElementAt({ 5 }) == false)) {
19428 std::string description = std::string("testMembershipOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(membership.getElementAt({ 5 }) == false)";
19429 std::cout << std::string("[FAIL] ") + description << std::endl;
19430 throw std::runtime_error(description);
19431 }
19432 // std::cout << "[OK] Membership testing (in1d) works correctly\n";
19433
19434 // Test isin (alias for in1d)
19435 auto isin_result = isin(test_array, test_values);
19436
19437 // Should be identical to in1d result
19438 for (size_t i = 0; i < membership.getSize(); ++i) {
19439 if (!(isin_result.getElementAt({ i }) == membership.getElementAt({ i }))) {
19440 std::string description = std::string("testMembershipOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(isin_result.getElementAt({ i }) == membership.getElementAt({ i }))";
19441 std::cout << std::string("[FAIL] ") + description << std::endl;
19442 throw std::runtime_error(description);
19443 }
19444 }
unique (np_test_1_all.cpp:6259)
6249 if (!(data.size() == 100)) {
6250 std::string description = std::string("test_data_generator_few_uniqueBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(data.size() == 100)";
6251 std::cout << std::string("[FAIL] ") + description << std::endl;
6252 throw std::runtime_error(description);
6253 }
6254
6255 // Count unique values
6256 std::vector<int> sorted_copy = data;
6257 std::sort(sorted_copy.begin(), sorted_copy.end());
6258 auto unique_end = std::unique(sorted_copy.begin(), sorted_copy.end());
6259 size_t unique_count = std::distance(sorted_copy.begin(), unique_end);
6260
6261 // Should have significantly fewer unique values than total elements
6262 if (!(unique_count < data.size() / 2)) {
6263 std::string description = std::string("test_data_generator_few_uniqueBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(unique_count < data.size() / 2)";
6264 std::cout << std::string("[FAIL] ") + description << std::endl;
6265 throw std::runtime_error(description);
6266 }
6267
6268 // std::cout << "Few unique data generation test passed. Unique values: " << unique_count << std::endl;
to_numpy (np_test_5_all.cpp:21373)
21363 if (errors == 0) {
21364 std::cout << "np_test_timedelta_components -> tests passed" << std::endl;
21365 }
21366 return errors;
21367}
21368
21369// =============================================================================
21370// Test 4: Total Conversion Properties
21371// =============================================================================
21372int np_test_timedelta_total_conversions() {
21373 int errors = 0;
21374
21375 numpy::Timedelta td(1, 12, 0); // 1 day 12 hours = 1.5 days = 36 hours
21376
21377 // total_seconds
21378 {
21379 double secs = td.total_seconds();
21380 double expected = (24 + 12) * 3600.0;
21381 if (std::abs(secs - expected) > 0.0001) {
21382 std::cout << "[FAIL] np_test_timedelta_total_conversions: total_seconds expected "
21383 << expected << ", got " << secs << std::endl;
to_string (np_test_1_all.cpp:454)
444 // Modify through different views
445 view1.setElementAt({0, 0}, 100);
446 view2.setElementAt({2, 1}, 200); // This is (1, 2) in original
447 view3.setElementAt({0, 0}, 300); // This is (1, 1) in original
448
449 // std::cout << "After modifications through multiple views:" << std::endl;
450 //original.printArray();
451
452 // Verify all changes are reflected
453 if (!(original.getElementAt({0, 0}) == 100)) {
454 std::string description = std::string("testAdvancedViewLifecycle():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({0, 0}) == 100)";
455 std::cout << std::string("[FAIL] ") + description << std::endl;
456 throw std::runtime_error(description);
457 }
458 if (!(original.getElementAt({1, 2}) == 200)) {
459 std::string description = std::string("testAdvancedViewLifecycle():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({1, 2}) == 200)";
460 std::cout << std::string("[FAIL] ") + description << std::endl;
461 throw std::runtime_error(description);
462 }
463 if (!(original.getElementAt({1, 1}) == 300)) {
464 std::string description = std::string("testAdvancedViewLifecycle():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({1, 1}) == 300)";
tolist (np_test_4_all.cpp:20046)
20036} // namespace numpy_tests
20037
20038/**
20039 * @file np_test_phase6.cpp
20040 * @brief Test suite for Phase 6A - HIGH PRIORITY NDArray methods and masked array utilities
20041 *
20042 * Tests:
20043 * - ndarray.nonzero() - Return indices of non-zero elements
20044 * - ndarray.tobytes() - Convert to bytes representation
20045 * - ndarray.tolist() - Convert to nested list string
20046 * - ndarray.fill() - Fill with scalar value (in-place)
20047 * - ndarray.item() - Get single element as scalar
20048 * - ndarray.itemset() - Set single element value
20049 * - ma.masked_all_like() - Create masked array with all elements masked
20050 *
20051 * Created: 2025-10-28
20052 * Status: Phase 6A Implementation - HIGH PRIORITY functions
20053 */
20054
20055#include "../numpy/np_ndarray.h"
append (np_test_2_all.cpp:7028)
7018 throw std::runtime_error(description);
7019 }
7020 if (!(deleted_dups.getElementAt({ 2 }) == 3)) {
7021 std::string description = std::string("testModificationOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(deleted_dups.getElementAt({ 2 }) == 3)";
7022 std::cout << std::string("[FAIL] ") + description << std::endl;
7023 throw std::runtime_error(description);
7024 }
7025 // std::cout << "[OK] Delete with duplicate indices works correctly\n";
7026
7027 // Test append
7028 auto appended = append(array, 100);
7029 if (!(appended.getSize() == array.getSize() + 1)) {
7030 std::string description = std::string("testModificationOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(appended.getSize() == array.getSize() + 1)";
7031 std::cout << std::string("[FAIL] ") + description << std::endl;
7032 throw std::runtime_error(description);
7033 }
7034 if (!(appended.getElementAt({ appended.getSize() - 1 }) == 100)) {
7035 std::string description = std::string("testModificationOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(appended.getElementAt({ appended.getSize() - 1 }) == 100)";
7036 std::cout << std::string("[FAIL] ") + description << std::endl;
7037 throw std::runtime_error(description);
7038 }
append (np_test_2_all.cpp:7028)
7018 throw std::runtime_error(description);
7019 }
7020 if (!(deleted_dups.getElementAt({ 2 }) == 3)) {
7021 std::string description = std::string("testModificationOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(deleted_dups.getElementAt({ 2 }) == 3)";
7022 std::cout << std::string("[FAIL] ") + description << std::endl;
7023 throw std::runtime_error(description);
7024 }
7025 // std::cout << "[OK] Delete with duplicate indices works correctly\n";
7026
7027 // Test append
7028 auto appended = append(array, 100);
7029 if (!(appended.getSize() == array.getSize() + 1)) {
7030 std::string description = std::string("testModificationOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(appended.getSize() == array.getSize() + 1)";
7031 std::cout << std::string("[FAIL] ") + description << std::endl;
7032 throw std::runtime_error(description);
7033 }
7034 if (!(appended.getElementAt({ appended.getSize() - 1 }) == 100)) {
7035 std::string description = std::string("testModificationOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(appended.getElementAt({ appended.getSize() - 1 }) == 100)";
7036 std::cout << std::string("[FAIL] ") + description << std::endl;
7037 throw std::runtime_error(description);
7038 }
delete_ (np_test_4_all.cpp:18411)
18401 }
18402
18403 // ============================================================================
18404 // SPRINT 1.2: ESSENTIAL OPERATIONS TESTS
18405 // ============================================================================
18406
18407 void np_test_phase1_delete() {
18408 std::cout << "========= delete_: element deletion ====";
18409
18410 auto arr = numpy::array({10, 20, 30, 40, 50});
18411 auto result = numpy::delete_(arr, 2); // Delete index 2 (value 30)
18412
18413 // Verify size reduced
18414 if (result.getSize() != 4) {
18415 std::cout << " [FAIL] : delete_ result size is " << result.getSize() << ", expected 4\n";
18416 throw std::runtime_error("delete_ failed: wrong size");
18417 }
18418
18419 // Verify correct element was removed
18420 if (result.getElementAt({0}) != 10 || result.getElementAt({1}) != 20 ||
18421 result.getElementAt({2}) != 40 || result.getElementAt({3}) != 50) {
delete_ (np_test_4_all.cpp:18411)
18401 }
18402
18403 // ============================================================================
18404 // SPRINT 1.2: ESSENTIAL OPERATIONS TESTS
18405 // ============================================================================
18406
18407 void np_test_phase1_delete() {
18408 std::cout << "========= delete_: element deletion ====";
18409
18410 auto arr = numpy::array({10, 20, 30, 40, 50});
18411 auto result = numpy::delete_(arr, 2); // Delete index 2 (value 30)
18412
18413 // Verify size reduced
18414 if (result.getSize() != 4) {
18415 std::cout << " [FAIL] : delete_ result size is " << result.getSize() << ", expected 4\n";
18416 throw std::runtime_error("delete_ failed: wrong size");
18417 }
18418
18419 // Verify correct element was removed
18420 if (result.getElementAt({0}) != 10 || result.getElementAt({1}) != 20 ||
18421 result.getElementAt({2}) != 40 || result.getElementAt({3}) != 50) {
insert (np_test_1_all.cpp:6990)
6980}
6981
6982void testBusinessDateRangeDateTime() {
6983 std::cout << "========= testBusinessDateRangeDateTime =======================";
6984
6985 datetime64 start("2024-03-11"); // Monday
6986 datetime64 end("2024-03-22"); // Friday (2 weeks later)
6987
6988 // Create holidays set
6989 std::set<datetime64> holidays;
6990 holidays.insert(datetime64("2024-03-15")); // Friday holiday
6991
6992 auto business_dates = createBusinessDateRange(start, end, holidays);
6993 // std::cout << "Business dates from " << start.toString()
6994 // << " to " << end.toString() << " (excluding 2024-03-15):" << std::endl;
6995 //business_dates.printArray();
6996
6997 std::cout << " -> tests passed" << std::endl;
6998}
6999
7000void testMonthRangeDateTime() {
repeat (np_test_1_all.cpp:18268)
18258 // Create test array
18259 auto array = createInt32Array({ 2, 3 }, 0);
18260 for (size_t i = 0; i < 2; ++i) {
18261 for (size_t j = 0; j < 3; ++j) {
18262 array.setElementAt({ i, j }, static_cast<int32_t>(i * 3 + j + 1));
18263 }
18264 }
18265
18266 // Test repeat without axis (flattened)
18267 auto repeated_flat = repeat(array, 2);
18268 if (!(repeated_flat.getShape()[0] == 12)) {
18269 std::string description = std::string("testRepeat():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(repeated_flat.getShape()[0] == 12)";
18270 std::cout << std::string("[FAIL] ") + description << std::endl;
18271 throw std::runtime_error(description);
18272 }
18273 if (!(repeated_flat.getElementAt({ 0 }) == 1)) {
18274 std::string description = std::string("testRepeat():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(repeated_flat.getElementAt({ 0 }) == 1)";
18275 std::cout << std::string("[FAIL] ") + description << std::endl;
18276 throw std::runtime_error(description);
18277 }
repeat (np_test_1_all.cpp:18268)
18258 // Create test array
18259 auto array = createInt32Array({ 2, 3 }, 0);
18260 for (size_t i = 0; i < 2; ++i) {
18261 for (size_t j = 0; j < 3; ++j) {
18262 array.setElementAt({ i, j }, static_cast<int32_t>(i * 3 + j + 1));
18263 }
18264 }
18265
18266 // Test repeat without axis (flattened)
18267 auto repeated_flat = repeat(array, 2);
18268 if (!(repeated_flat.getShape()[0] == 12)) {
18269 std::string description = std::string("testRepeat():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(repeated_flat.getShape()[0] == 12)";
18270 std::cout << std::string("[FAIL] ") + description << std::endl;
18271 throw std::runtime_error(description);
18272 }
18273 if (!(repeated_flat.getElementAt({ 0 }) == 1)) {
18274 std::string description = std::string("testRepeat():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(repeated_flat.getElementAt({ 0 }) == 1)";
18275 std::cout << std::string("[FAIL] ") + description << std::endl;
18276 throw std::runtime_error(description);
18277 }
astype (np_test_3_all.cpp:796)
786 auto ufunc_square = numpy::frompyfunc(square, 1, 1);
787
788 numpy::NDArray<double> arr({ 3 });
789 arr.setElementAt({ 0 }, 2.0);
790 arr.setElementAt({ 1 }, 3.0);
791 arr.setElementAt({ 2 }, 4.0);
792
793 auto result = ufunc_square(arr);
794
795 // Convert back to double to check values
796 auto typed_result = numpy::astype<double>(result);
797 if (std::abs(typed_result.getElementAt({ 0 }) - 4.0) > 1e-10 ||
798 std::abs(typed_result.getElementAt({ 1 }) - 9.0) > 1e-10 ||
799 std::abs(typed_result.getElementAt({ 2 }) - 16.0) > 1e-10) {
800 std::cout << "[FAIL] Basic frompyfunc creation failed";
801 return 1;
802 }
803
804 // std::cout << "[OK] Basic frompyfunc creation works correctly" << std::endl;
805 std::cout << " -> tests passed" << std::endl;
806 return 0;
copy (np_test_1_all.cpp:9812)
9802 //original.printArray();
9803
9804 // The modification should be at position (1,1) in original
9805 if (!(original.getElementAt({1, 1}) == 9999)) {
9806 std::string description = std::string("testSliceCopyVsViewMemoryOwnership():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({1, 1}) == 9999)";
9807 std::cout << std::string("[FAIL] ") + description << std::endl;
9808 throw std::runtime_error(description);
9809 }
9810 // std::cout << "[OK] Slice view shares memory with original";
9811
9812 // Test slice copy (should be independent)
9813 auto slice_copy = original.sliceArray({{2, 4}, {2, 4}});
9814 // std::cout << "Slice copy [2:4, 2:4]:";
9815 //slice_copy.printArray();
9816
9817 slice_copy.setElementAt({0, 0}, 7777);
9818
9819 // std::cout << "After modifying slice copy at (0,0):" << std::endl;
9820 // std::cout << "Original array:" << std::endl;
9821 //original.printArray();
9822 // std::cout << "Slice copy:" << std::endl;
view (np_test_1_all.cpp:440)
430 for (size_t i = 0; i < 3; ++i) {
431 for (size_t j = 0; j < 3; ++j) {
432 original.setElementAt({i, j}, static_cast<int32_t>(i * 3 + j));
433 }
434 }
435
436 // std::cout << "Original array:" << std::endl;
437 //original.printArray();
438
439 // Create multiple views
440 auto view1 = original.view();
441 auto view2 = original.transposeView();
442 auto view3 = original.sliceView({{1, 3}, {1, 3}});
443
444 // Modify through different views
445 view1.setElementAt({0, 0}, 100);
446 view2.setElementAt({2, 1}, 200); // This is (1, 2) in original
447 view3.setElementAt({0, 0}, 300); // This is (1, 1) in original
448
449 // std::cout << "After modifications through multiple views:" << std::endl;
450 //original.printArray();
is_numeric (np_test_1_all.cpp:7511)
7501 std::cout << " -> tests passed" << std::endl;
7502}
7503
7504void testNumericConversionStringTypes() {
7505 std::cout << "========= testNumericStringOperationsStringTypes =======================";
7506
7507 str32 num_str1("123.45");
7508 str32 num_str2("67.89");
7509 str32 not_num("hello");
7510
7511 // std::cout << "String '" << num_str1 << "' is numeric: " << is_numeric(num_str1) << std::endl;
7512 // std::cout << "String '" << not_num << "' is numeric: " << is_numeric(not_num) << std::endl;
7513
7514 if (is_numeric(num_str1)) {
7515 double val1 = to_numeric(num_str1);
7516 // std::cout << "Numeric value of '" << num_str1 << "': " << val1 << std::endl;
7517 }
7518
7519 if (is_numeric(num_str2)) {
7520 double val2 = to_numeric(num_str2);
7521 // std::cout << "Numeric value of '" << num_str2 << "': " << val2 << std::endl;
argmax (np_test_3_all.cpp:20521)
20511 auto result = numpy::argmin(arr);
20512 // Should return 1 (index of (1,5))
20513 if (result.getElementAt({0}) != 1) {
20514 throw std::runtime_error("argmin() failed: expected index 1");
20515 }
20516 std::cout << "PASSED\n";
20517}
20518
20519void test_argmax_complex128() {
20520 std::cout << " Testing argmax() with complex128... ";
20521 numpy::NDArray<complex128> arr({3});
20522 arr.setElementAt({0}, complex128(3.0, 1.0)); // index 0 - largest
20523 arr.setElementAt({1}, complex128(1.0, 5.0)); // index 1
20524 arr.setElementAt({2}, complex128(2.0, 2.0)); // index 2
20525
20526 auto result = numpy::argmax(arr);
20527 // Should return 0 (index of (3,1))
20528 if (result.getElementAt({0}) != 0) {
20529 throw std::runtime_error("argmax() failed: expected index 0");
20530 }
argmin (np_test_3_all.cpp:20506)
20496 if (min_result.getElementAt({0}).real() != 2.0 || min_result.getElementAt({0}).imag() != 1.0) {
20497 throw std::runtime_error("min() tie-breaking failed: expected (2,1)");
20498 }
20499 if (max_result.getElementAt({0}).real() != 2.0 || max_result.getElementAt({0}).imag() != 5.0) {
20500 throw std::runtime_error("max() tie-breaking failed: expected (2,5)");
20501 }
20502 std::cout << "PASSED\n";
20503}
20504
20505void test_argmin_complex128() {
20506 std::cout << " Testing argmin() with complex128... ";
20507 numpy::NDArray<complex128> arr({3});
20508 arr.setElementAt({0}, complex128(3.0, 1.0)); // index 0
20509 arr.setElementAt({1}, complex128(1.0, 5.0)); // index 1 - smallest
20510 arr.setElementAt({2}, complex128(2.0, 2.0)); // index 2
20511
20512 auto result = numpy::argmin(arr);
20513 // Should return 1 (index of (1,5))
20514 if (result.getElementAt({0}) != 1) {
20515 throw std::runtime_error("argmin() failed: expected index 1");
20516 }
arr (np_test_1_all.cpp:1388)
1378 std::cout << std::string("[FAIL] ") + description << std::endl;
1379 throw std::runtime_error(description);
1380 }
1381 if (!(*str_obj.get_as<std::string>() == "hello")) {
1382 std::string description = std::string("testObjectConstruction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(*str_obj.get_as<std::string>() == \"hello\")";
1383 std::cout << std::string("[FAIL] ") + description << std::endl;
1384 throw std::runtime_error(description);
1385 }
1386
1387 // Test NDArray construction
1388 NDArray<double> arr({2, 2}, 3.14);
1389 object_ arr_obj(arr);
1390 if (!(!arr_obj.is_null())) {
1391 std::string description = std::string("testObjectConstruction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(!arr_obj.is_null())";
1392 std::cout << std::string("[FAIL] ") + description << std::endl;
1393 throw std::runtime_error(description);
1394 }
1395 if (!(arr_obj.is_array())) {
1396 std::string description = std::string("testObjectConstruction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(arr_obj.is_array())";
1397 std::cout << std::string("[FAIL] ") + description << std::endl;
1398 throw std::runtime_error(description);
arr (np_test_1_all.cpp:1388)
1378 std::cout << std::string("[FAIL] ") + description << std::endl;
1379 throw std::runtime_error(description);
1380 }
1381 if (!(*str_obj.get_as<std::string>() == "hello")) {
1382 std::string description = std::string("testObjectConstruction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(*str_obj.get_as<std::string>() == \"hello\")";
1383 std::cout << std::string("[FAIL] ") + description << std::endl;
1384 throw std::runtime_error(description);
1385 }
1386
1387 // Test NDArray construction
1388 NDArray<double> arr({2, 2}, 3.14);
1389 object_ arr_obj(arr);
1390 if (!(!arr_obj.is_null())) {
1391 std::string description = std::string("testObjectConstruction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(!arr_obj.is_null())";
1392 std::cout << std::string("[FAIL] ") + description << std::endl;
1393 throw std::runtime_error(description);
1394 }
1395 if (!(arr_obj.is_array())) {
1396 std::string description = std::string("testObjectConstruction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(arr_obj.is_array())";
1397 std::cout << std::string("[FAIL] ") + description << std::endl;
1398 throw std::runtime_error(description);
as_unit (np_test_5_all.cpp:22866)
22856 if (iso_week < 1 || iso_week > 53) { pass = false; fail_msg = "ISO week range"; }
22857 if (iso_day < 1 || iso_day > 7) { pass = false; fail_msg = "ISO day range 1-7"; }
22858
22859 // isoweekday (1=Monday, 7=Sunday)
22860 int isowd = ts.isoweekday();
22861 if (isowd != 6) { pass = false; fail_msg = "2024-06-15 is Saturday = 6"; }
22862
22863 // weekday (0=Monday, 6=Sunday)
22864 int wd = ts.weekday();
22865 if (wd != 5) { pass = false; fail_msg = "2024-06-15 is Saturday = 5"; }
22866
22867 if (!pass) {
22868 std::cout << " [FAIL] : in np_test_timestamp_calendar() : " << fail_msg;
22869 throw std::runtime_error("np_test_timestamp_calendar failed: " + fail_msg);
22870 }
22871
22872 std::cout << " -> tests passed" << std::endl;
22873 }
22874
22875 // ============================================================================
22876 // ROUNDING TESTS
clone (np_test_1_all.cpp:24942)
24932 }
24933
24934 // Test reproducibility
24935 numpy::random::PCG64DXSM rng2(11111);
24936 if (rng2.next_uint64() != val) {
24937 std::cout << " [FAIL] : in np_test_bitgen_pcg64dxsm() : not reproducible";
24938 throw std::runtime_error("np_test_bitgen_pcg64dxsm failed");
24939 }
24940
24941 // Test clone
24942 auto cloned = rng.clone();
24943 if (cloned->name() != "PCG64DXSM") {
24944 std::cout << " [FAIL] : in np_test_bitgen_pcg64dxsm() : clone name incorrect";
24945 throw std::runtime_error("np_test_bitgen_pcg64dxsm failed");
24946 }
24947
24948 std::cout << " -> tests passed" << std::endl;
24949 }
24950
24951 // ============================================================================
24952 // PHILOX TESTS
components (np_test_1_all.cpp:27398)
27388 // Check that results are strings (basic validation)
27389 std::string str0 = result.getElementAt({ 0 });
27390 std::string str1 = result.getElementAt({ 1 });
27391
27392 if (str0.empty() || str1.empty()) {
27393 std::cout << " [FAIL] : in test_datetime_as_string_iso() : empty string result";
27394 throw std::runtime_error("datetime_as_string ISO test failed");
27395 }
27396
27397 // Check format contains date components (YYYY-MM-DD)
27398 if (str0.find("2024") == std::string::npos || str0.find("01") == std::string::npos) {
27399 std::cout << " [FAIL] : in test_datetime_as_string_iso() : incorrect ISO format";
27400 throw std::runtime_error("datetime_as_string ISO test failed");
27401 }
27402
27403 std::cout << " -> tests passed" << std::endl;
27404 }
27405
27406 void test_datetime_as_string_custom() {
27407 std::cout << "========= datetime_as_string: custom format =======================";
days (np_test_1_all.cpp:28638)
28628 }
28629
28630 void np_test_datetime_business_date_range() {
28631 std::cout << "========= businessDateRange: business days only =======================";
28632
28633 // Get business days in first week of January 2024
28634 auto start_dt = numpy::datetime64("2024-01-01");
28635 auto end_dt = numpy::datetime64("2024-01-07");
28636 auto biz_dates = numpy::datetime_utils::businessDateRange(start_dt, end_dt);
28637
28638 // Should have between 3-5 business days (accounting for holidays)
28639 bool passed = (biz_dates.size() >= 3 && biz_dates.size() <= 5);
28640
28641 if (!passed) {
28642 std::cout << " [FAIL] : in np_test_datetime_business_date_range() : Got " << biz_dates.size() << " business days";
28643 throw std::runtime_error("np_test_datetime_business_date_range failed");
28644 }
28645
28646 std::cout << " -> tests passed" << std::endl;
28647 }
diff (np_test_2_all.cpp:4947)
4937 // Test matrix addition and subtraction
4938 numpy::Matrix<double> a("1 2; 3 4");
4939 numpy::Matrix<double> b("5 6; 7 8");
4940
4941 auto sum = a + b;
4942 assert_test(std::abs(sum(0, 0) - 6.0) < 1e-10, "Matrix addition");
4943 assert_test(std::abs(sum(1, 1) - 12.0) < 1e-10, "Matrix addition");
4944
4945 auto diff = b - a;
4946 assert_test(std::abs(diff(0, 0) - 4.0) < 1e-10, "Matrix subtraction");
4947 assert_test(std::abs(diff(1, 1) - 4.0) < 1e-10, "Matrix subtraction");
4948
4949 // Test matrix multiplication (not element-wise!)
4950 auto product = a * b;
4951 assert_test(std::abs(product(0, 0) - 19.0) < 1e-10, "Matrix multiplication 0,0");
4952 assert_test(std::abs(product(0, 1) - 22.0) < 1e-10, "Matrix multiplication 0,1");
4953 assert_test(std::abs(product(1, 0) - 43.0) < 1e-10, "Matrix multiplication 1,0");
4954 assert_test(std::abs(product(1, 1) - 50.0) < 1e-10, "Matrix multiplication 1,1");
4955
4956 // Test scalar multiplication
difference (np_test_1_all.cpp:19333)
19323 std::cout << std::string("[FAIL] ") + description << std::endl;
19324 throw std::runtime_error(description);
19325 }
19326 if (!(difference.getElementAt({ 2 }) == 5)) {
19327 std::string description = std::string("testSetDifferenceOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(difference.getElementAt({ 2 }) == 5)";
19328 std::cout << std::string("[FAIL] ") + description << std::endl;
19329 throw std::runtime_error(description);
19330 }
19331 // std::cout << "[OK] Set difference works correctly\n";
19332
19333 // Test symmetric difference (XOR)
19334 auto xor_result = setxor1d(arr1, arr2);
19335
19336 if (!(xor_result.getSize() == 4)) {
19337 std::string description = std::string("testSetDifferenceOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(xor_result.getSize() == 4)";
19338 std::cout << std::string("[FAIL] ") + description << std::endl;
19339 throw std::runtime_error(description);
19340 }
19341 if (!(xor_result.getElementAt({ 0 }) == 1)) {
19342 std::string description = std::string("testSetDifferenceOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(xor_result.getElementAt({ 0 }) == 1)";
19343 std::cout << std::string("[FAIL] ") + description << std::endl;
float_arr (np_test_4_all.cpp:23233)
23223 numpy::NDArray<numpy::bool_> mask1 = numpy::ma::make_mask(int_arr);
23224
23225 if (mask1.item(0) != numpy::bool_(false) || mask1.item(1) != numpy::bool_(true) ||
23226 mask1.item(2) != numpy::bool_(true) || mask1.item(3) != numpy::bool_(false) ||
23227 mask1.item(4) != numpy::bool_(true)) {
23228 throw std::runtime_error("make_mask(int array) failed - incorrect boolean conversion");
23229 }
23230 // std::cout << " OK: Integer array conversion (non-zero = True)\n";
23231
23232 // Test 2: Float array
23233 numpy::NDArray<numpy::float64> float_arr({4});
23234 float_arr.setElementAt({0}, 1.0);
23235 float_arr.setElementAt({1}, 0.0);
23236 float_arr.setElementAt({2}, 2.5);
23237 float_arr.setElementAt({3}, 0.0);
23238
23239 numpy::NDArray<numpy::bool_> mask2 = numpy::ma::make_mask(float_arr);
23240
23241 if (mask2.item(0) != numpy::bool_(true) || mask2.item(1) != numpy::bool_(false) ||
23242 mask2.item(2) != numpy::bool_(true) || mask2.item(3) != numpy::bool_(false)) {
23243 throw std::runtime_error("make_mask(float array) failed");
format (main.cpp:22)
12int main() {
13 // Automatically log all output to temp/test_output.log
14 numpy::TestLogger logger("temp/test_output.log");
15
16 int res = 0;
17 int res1 = 0;
18 std::string resS = "";
19
20 // call all the tests
21 res1 = numpy_tests::np_test_main();
22 resS += std::format(" np_test_main: {} errors\n", res1);
23 res += res1;
24
25 std::cout << "\n------------------------- main --------------------------------------------\n";
26 std::cout << std::endl << "All tests completed. Nb errors = " << res << std::endl;
27 std::cout << "Details: \n" << resS;
28 std::cout << "\n---------------------------------------------------------------------------\n";
29 return res;
30}
idx (np_test_2_all.cpp:7377)
7367 throw std::runtime_error(description);
7368 }
7369
7370 auto [row, col] = to_2d_indices(13, 5);
7371 if (!(row == 2 && col == 3)) {
7372 std::string description = std::string("test_matrix_operations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(row == 2 && col == 3)";
7373 std::cout << std::string("[FAIL] ") + description << std::endl;
7374 throw std::runtime_error(description);
7375 }
7376
7377 // std::cout << "Matrix indexing: (2,3) -> " << idx << " -> (" << row << "," << col << ")" << std::endl;
7378
7379 // Test vector operations
7380 double vec[3] = { 3.0, 4.0, 0.0 };
7381 double norm = vector_norm_2(vec, 3);
7382 if (!(std::abs(norm - 5.0) < 1e-10)) {
7383 std::string description = std::string("test_matrix_operations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(std::abs(norm - 5.0) < 1e-10)";
7384 std::cout << std::string("[FAIL] ") + description << std::endl;
7385 throw std::runtime_error(description);
7386 }
7387 // std::cout << "Vector norm [3,4,0]: " << norm << std::endl;
idx (np_test_2_all.cpp:7377)
7367 throw std::runtime_error(description);
7368 }
7369
7370 auto [row, col] = to_2d_indices(13, 5);
7371 if (!(row == 2 && col == 3)) {
7372 std::string description = std::string("test_matrix_operations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(row == 2 && col == 3)";
7373 std::cout << std::string("[FAIL] ") + description << std::endl;
7374 throw std::runtime_error(description);
7375 }
7376
7377 // std::cout << "Matrix indexing: (2,3) -> " << idx << " -> (" << row << "," << col << ")" << std::endl;
7378
7379 // Test vector operations
7380 double vec[3] = { 3.0, 4.0, 0.0 };
7381 double norm = vector_norm_2(vec, 3);
7382 if (!(std::abs(norm - 5.0) < 1e-10)) {
7383 std::string description = std::string("test_matrix_operations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(std::abs(norm - 5.0) < 1e-10)";
7384 std::cout << std::string("[FAIL] ") + description << std::endl;
7385 throw std::runtime_error(description);
7386 }
7387 // std::cout << "Vector norm [3,4,0]: " << norm << std::endl;
idx (np_test_2_all.cpp:7377)
7367 throw std::runtime_error(description);
7368 }
7369
7370 auto [row, col] = to_2d_indices(13, 5);
7371 if (!(row == 2 && col == 3)) {
7372 std::string description = std::string("test_matrix_operations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(row == 2 && col == 3)";
7373 std::cout << std::string("[FAIL] ") + description << std::endl;
7374 throw std::runtime_error(description);
7375 }
7376
7377 // std::cout << "Matrix indexing: (2,3) -> " << idx << " -> (" << row << "," << col << ")" << std::endl;
7378
7379 // Test vector operations
7380 double vec[3] = { 3.0, 4.0, 0.0 };
7381 double norm = vector_norm_2(vec, 3);
7382 if (!(std::abs(norm - 5.0) < 1e-10)) {
7383 std::string description = std::string("test_matrix_operations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(std::abs(norm - 5.0) < 1e-10)";
7384 std::cout << std::string("[FAIL] ") + description << std::endl;
7385 throw std::runtime_error(description);
7386 }
7387 // std::cout << "Vector norm [3,4,0]: " << norm << std::endl;
int_arr (np_test_3_all.cpp:1209)
1199 }
1200
1201 // Test 12: Type conversion utilities
1202 int np_test_frompyfunc_type_conversion() {
1203 std::cout << "========= np_test_frompyfunc_type_conversion =======================";
1204
1205 try {
1206 auto int_func = [](int x) { return x * 2; };
1207 auto ufunc_int = numpy::frompyfunc(int_func, 1, 1);
1208
1209 numpy::NDArray<int> int_arr({ 3 });
1210 int_arr.setElementAt({ 0 }, 1);
1211 int_arr.setElementAt({ 1 }, 2);
1212 int_arr.setElementAt({ 2 }, 3);
1213
1214 auto obj_result = ufunc_int(int_arr);
1215 auto typed_back = numpy::astype<int>(obj_result);
1216
1217 if (typed_back.getElementAt({ 0 }) != 2 ||
1218 typed_back.getElementAt({ 1 }) != 4 ||
1219 typed_back.getElementAt({ 2 }) != 6) {
join (np_test_5_all.cpp:1341)
1331 // ============================================================================
1332
1333 void np_test_string_join() {
1334 std::cout << "========= join operation =======================";
1335
1336 // Join takes a separator string and vector of CharArrays
1337 std::vector<numpy::CharArray<32>> arrays;
1338 arrays.push_back(numpy::char_::array<32>({ "a", "b", "c" }));
1339 arrays.push_back(numpy::char_::array<32>({ "x", "y", "z" }));
1340
1341 auto result = numpy::char_::join("-", arrays);
1342
1343 bool passed = true;
1344
1345 if (!passed) {
1346 std::cout << " [FAIL] : in np_test_string_join() : join operation failed";
1347 throw std::runtime_error("np_test_string_join failed");
1348 }
1349
1350 std::cout << " -> tests passed" << std::endl;
1351 }
microseconds (np_test_2_all.cpp:4731)
4721 }
4722
4723 // Write thread-specific pattern
4724 for (size_t j = 0; j < size; ++j) {
4725 ptr[j] = thread_id * 1000 + static_cast<int>(j);
4726 }
4727
4728 allocations.push_back({ ptr, size });
4729
4730 // Small delay to increase chance of concurrent access
4731 std::this_thread::sleep_for(std::chrono::microseconds(1));
4732 }
4733
4734 // Verify data integrity
4735 for (auto& alloc : allocations) {
4736 for (size_t j = 0; j < alloc.second; ++j) {
4737 if (!(alloc.first[j] == thread_id * 1000 + static_cast<int>(j))) {
4738 std::string description = std::string("test_concurrent_allocations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(alloc.first[j] == thread_id * 1000 + static_cast<int>(j))";
4739 std::cout << std::string("[FAIL] ") + description << std::endl;
4740 throw std::runtime_error(description);
4741 }
result (np_test_1_all.cpp:6090)
6080 throw std::runtime_error(description);
6081 }
6082 // std::cout << "Timer restart test passed. Second: " << second_elapsed << " ms" << std::endl;
6083
6084 std::cout << " -> tests passed" << std::endl;
6085}
6086
6087void test_benchmark_result_storageBenchmarkSorting() {
6088 std::cout << "========= test_benchmark_result_storage =======================";
6089
6090 BenchmarkResult result("TestAlgorithm", 1000, 15.5, true, 4096);
6091
6092 if (!(result.algorithm_name == "TestAlgorithm")) {
6093 std::string description = std::string("test_benchmark_result_storageBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result.algorithm_name == \"TestAlgorithm\")";
6094 std::cout << std::string("[FAIL] ") + description << std::endl;
6095 throw std::runtime_error(description);
6096 }
6097 if (!(result.array_size == 1000)) {
6098 std::string description = std::string("test_benchmark_result_storageBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result.array_size == 1000)";
6099 std::cout << std::string("[FAIL] ") + description << std::endl;
6100 throw std::runtime_error(description);
seconds (np_test_5_all.cpp:21240)
21230 // From value and unit - static factory for fractional values
21231 {
21232 numpy::Timedelta td_frac = numpy::Timedelta::from_value(1.5, "h"); // 1.5 hours = 90 minutes
21233 std::cout << " Timedelta::from_value(1.5, 'h') = " << td_frac.total_minutes() << " minutes" << std::endl;
21234 if (std::abs(td_frac.total_minutes() - 90.0) > 0.0001) {
21235 std::cout << "[FAIL] np_test_timedelta_constructors: from_value(1.5, 'h') expected 90 minutes, got "
21236 << td_frac.total_minutes() << std::endl;
21237 errors++;
21238 }
21239
21240 numpy::Timedelta td_frac2 = numpy::Timedelta::from_value(0.5, "D"); // 0.5 days = 12 hours
21241 std::cout << " Timedelta::from_value(0.5, 'D') = " << td_frac2.total_hours() << " hours" << std::endl;
21242 if (std::abs(td_frac2.total_hours() - 12.0) > 0.0001) {
21243 std::cout << "[FAIL] np_test_timedelta_constructors: from_value(0.5, 'D') expected 12 hours, got "
21244 << td_frac2.total_hours() << std::endl;
21245 errors++;
21246 }
21247 }
21248
21249 if (errors == 0) {
21250 std::cout << "np_test_timedelta_constructors -> tests passed" << std::endl;
shift (np_test_1_all.cpp:2761)
2751 throw std::runtime_error(description);
2752 }
2753 if (!(left_array.getElementAt({3}) == 16384)) {
2754 std::string description = std::string("testBitShifts():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(left_array.getElementAt({3}) == 16384)";
2755 std::cout << std::string("[FAIL] ") + description << std::endl;
2756 throw std::runtime_error(description);
2757 }
2758
2759 // std::cout << "[OK] Left shift by array\n";
2760
2761 // Test excessive shift (should result in 0)
2762 auto excessive_left = left_shift(arr, 20); // More than 16 bits
2763 if (!(excessive_left.getElementAt({0}) == 0)) {
2764 std::string description = std::string("testBitShifts():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(excessive_left.getElementAt({0}) == 0)";
2765 std::cout << std::string("[FAIL] ") + description << std::endl;
2766 throw std::runtime_error(description);
2767 }
2768 if (!(excessive_left.getElementAt({1}) == 0)) {
2769 std::string description = std::string("testBitShifts():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(excessive_left.getElementAt({1}) == 0)";
2770 std::cout << std::string("[FAIL] ") + description << std::endl;
2771 throw std::runtime_error(description);
slice (np_test_1_all.cpp:13009)
12999 // Test substr valid start but excessive length
13000 auto result3 = str1.substr(2, 10); // start=2, len=10 on string of length 5
13001 if (!(result3.to_string() == "llo")) {
13002 std::string description = std::string("test_variable_string_out_of_bounds():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result3.to_string() == \"llo\")";
13003 std::cout << std::string("[FAIL] ") + description << std::endl;
13004 throw std::runtime_error(description);
13005 }
13006 // std::cout << "[OK] vstring_ substr(2, 10) on 'hello' returns 'llo'\n";
13007
13008 // Test slice method with out-of-bounds
13009 auto result4 = str1.slice(10, 15); // slice[10:15] on string of length 5
13010 if (!(result4.to_string() == "")) {
13011 std::string description = std::string("test_variable_string_out_of_bounds():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result4.to_string() == \"\")";
13012 std::cout << std::string("[FAIL] ") + description << std::endl;
13013 throw std::runtime_error(description);
13014 }
13015 // std::cout << "[OK] vstring_ slice(10, 15) on 'hello' returns empty string\n";
13016
13017 // Test slice with negative indices
13018 auto result5 = str1.slice(-2, -1); // slice[-2:-1] should be "l"
13019 if (!(result5.to_string() == "l")) {