Timedelta#
-
class numpy::Timedelta#
numpy C++ class.
Example#
#include <numpy/np_ndarray.h>
using namespace numpy;
// Use Timedelta
Timedelta obj;
// ... operations ...
Constructors#
Signature |
Location |
Example |
|---|---|---|
|
NP_TIMEDELTA.H:233 |
|
|
NP_TIMEDELTA.H:238 |
|
|
NP_TIMEDELTA.H:253 |
|
|
NP_TIMEDELTA.H:261 |
|
|
NP_TIMEDELTA.H:273 |
|
|
NP_TIMEDELTA.H:308 |
Operators#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
Timedelta |
NP_TIMEDELTA.H:680 |
|
|
Timedelta |
NP_TIMEDELTA.H:685 |
|
|
Timedelta & |
NP_TIMEDELTA.H:690 |
|
|
Timedelta & |
NP_TIMEDELTA.H:700 |
|
|
Timedelta |
NP_TIMEDELTA.H:711 |
|
|
Timedelta |
NP_TIMEDELTA.H:717 |
|
|
Timedelta |
NP_TIMEDELTA.H:722 |
|
|
Timedelta |
NP_TIMEDELTA.H:726 |
|
|
Timedelta & |
NP_TIMEDELTA.H:731 |
|
|
Timedelta & |
NP_TIMEDELTA.H:739 |
|
|
Timedelta & |
NP_TIMEDELTA.H:743 |
|
|
Timedelta |
NP_TIMEDELTA.H:752 |
|
|
Timedelta |
NP_TIMEDELTA.H:757 |
|
|
Timedelta |
NP_TIMEDELTA.H:761 |
|
|
Timedelta & |
NP_TIMEDELTA.H:766 |
|
|
Timedelta & |
NP_TIMEDELTA.H:776 |
|
|
Timedelta & |
NP_TIMEDELTA.H:780 |
|
|
double |
NP_TIMEDELTA.H:791 |
|
|
Timedelta |
NP_TIMEDELTA.H:799 |
|
|
Timedelta |
NP_TIMEDELTA.H:829 |
|
|
Timedelta |
NP_TIMEDELTA.H:834 |
|
|
bool |
NP_TIMEDELTA.H:843 |
|
|
bool |
NP_TIMEDELTA.H:849 |
|
|
bool |
NP_TIMEDELTA.H:855 |
|
|
bool |
NP_TIMEDELTA.H:860 |
|
|
bool |
NP_TIMEDELTA.H:865 |
|
|
bool |
NP_TIMEDELTA.H:870 |
Construction#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
Timedelta |
NP_TIMEDELTA.H:268 |
Statistics#
Math Operations#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
Timedelta |
NP_TIMEDELTA.H:807 |
|
|
Timedelta |
NP_TIMEDELTA.H:603 |
|
|
Timedelta |
NP_TIMEDELTA.H:585 |
|
|
Timedelta |
NP_TIMEDELTA.H:624 |
Linear Algebra#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
void |
NP_TIMEDELTA.H:62 |
I/O#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
std::chrono::nanoseconds |
NP_TIMEDELTA.H:476 |
|
|
Duration |
NP_TIMEDELTA.H:484 |
|
|
int64_t |
NP_TIMEDELTA.H:469 |
|
|
timedelta64 |
NP_TIMEDELTA.H:438 |
Other Methods#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
Timedelta |
NP_TIMEDELTA.H:318 |
|
|
Timedelta |
NP_TIMEDELTA.H:656 |
|
|
int64_t |
NP_TIMEDELTA.H:402 |
|
|
std::string |
NP_TIMEDELTA.H:566 |
|
|
void |
NP_TIMEDELTA.H:66 |
|
|
int64_t |
NP_TIMEDELTA.H:359 |
|
|
int64_t |
NP_TIMEDELTA.H:354 |
|
|
int32_t |
NP_TIMEDELTA.H:365 |
|
|
bool |
NP_TIMEDELTA.H:340 |
|
|
std::string |
NP_TIMEDELTA.H:529 |
|
|
int32_t |
NP_TIMEDELTA.H:389 |
|
|
int32_t |
NP_TIMEDELTA.H:383 |
|
|
int64_t |
NP_TIMEDELTA.H:112 |
|
|
int64_t |
NP_TIMEDELTA.H:99 |
|
|
Timedelta |
NP_TIMEDELTA.H:332 |
|
|
int32_t |
NP_TIMEDELTA.H:377 |
|
|
std::string |
NP_TIMEDELTA.H:497 |
|
|
double |
NP_TIMEDELTA.H:423 |
|
|
double |
NP_TIMEDELTA.H:418 |
|
|
double |
NP_TIMEDELTA.H:413 |
|
|
double |
NP_TIMEDELTA.H:428 |
|
|
double |
NP_TIMEDELTA.H:408 |
|
|
int64_t |
NP_TIMEDELTA.H:349 |
Code Examples#
The following examples are extracted from the test suite.
Timedelta (np_test_5_all.cpp:21164)
21154 if (std::abs(td.total_hours() - 2.0) > 0.0001) {
21155 std::cout << "[FAIL] np_test_timedelta_constructors: from chrono expected 2 hours, got "
21156 << td.total_hours() << std::endl;
21157 errors++;
21158 }
21159 }
21160
21161 // From value and unit (pandas-style) - int64_t version
21162 {
21163 numpy::Timedelta td_hours(5, "h"); // 5 hours
21164 std::cout << " Timedelta(5, 'h') = " << td_hours.total_hours() << " hours" << std::endl;
21165 if (std::abs(td_hours.total_hours() - 5.0) > 0.0001) {
21166 std::cout << "[FAIL] np_test_timedelta_constructors: Timedelta(5, 'h') expected 5 hours, got "
21167 << td_hours.total_hours() << std::endl;
21168 errors++;
21169 }
21170
21171 numpy::Timedelta td_days(3, "D"); // 3 days
21172 std::cout << " Timedelta(3, 'D') = " << td_days.days() << " days" << std::endl;
21173 if (td_days.days() != 3) {
21174 std::cout << "[FAIL] np_test_timedelta_constructors: Timedelta(3, 'D') expected 3 days, got "
Timedelta (np_test_5_all.cpp:21164)
21154 if (std::abs(td.total_hours() - 2.0) > 0.0001) {
21155 std::cout << "[FAIL] np_test_timedelta_constructors: from chrono expected 2 hours, got "
21156 << td.total_hours() << std::endl;
21157 errors++;
21158 }
21159 }
21160
21161 // From value and unit (pandas-style) - int64_t version
21162 {
21163 numpy::Timedelta td_hours(5, "h"); // 5 hours
21164 std::cout << " Timedelta(5, 'h') = " << td_hours.total_hours() << " hours" << std::endl;
21165 if (std::abs(td_hours.total_hours() - 5.0) > 0.0001) {
21166 std::cout << "[FAIL] np_test_timedelta_constructors: Timedelta(5, 'h') expected 5 hours, got "
21167 << td_hours.total_hours() << std::endl;
21168 errors++;
21169 }
21170
21171 numpy::Timedelta td_days(3, "D"); // 3 days
21172 std::cout << " Timedelta(3, 'D') = " << td_days.days() << " days" << std::endl;
21173 if (td_days.days() != 3) {
21174 std::cout << "[FAIL] np_test_timedelta_constructors: Timedelta(3, 'D') expected 3 days, got "
Timedelta (np_test_5_all.cpp:21164)
21154 if (std::abs(td.total_hours() - 2.0) > 0.0001) {
21155 std::cout << "[FAIL] np_test_timedelta_constructors: from chrono expected 2 hours, got "
21156 << td.total_hours() << std::endl;
21157 errors++;
21158 }
21159 }
21160
21161 // From value and unit (pandas-style) - int64_t version
21162 {
21163 numpy::Timedelta td_hours(5, "h"); // 5 hours
21164 std::cout << " Timedelta(5, 'h') = " << td_hours.total_hours() << " hours" << std::endl;
21165 if (std::abs(td_hours.total_hours() - 5.0) > 0.0001) {
21166 std::cout << "[FAIL] np_test_timedelta_constructors: Timedelta(5, 'h') expected 5 hours, got "
21167 << td_hours.total_hours() << std::endl;
21168 errors++;
21169 }
21170
21171 numpy::Timedelta td_days(3, "D"); // 3 days
21172 std::cout << " Timedelta(3, 'D') = " << td_days.days() << " days" << std::endl;
21173 if (td_days.days() != 3) {
21174 std::cout << "[FAIL] np_test_timedelta_constructors: Timedelta(3, 'D') expected 3 days, got "
Timedelta (np_test_5_all.cpp:21164)
21154 if (std::abs(td.total_hours() - 2.0) > 0.0001) {
21155 std::cout << "[FAIL] np_test_timedelta_constructors: from chrono expected 2 hours, got "
21156 << td.total_hours() << std::endl;
21157 errors++;
21158 }
21159 }
21160
21161 // From value and unit (pandas-style) - int64_t version
21162 {
21163 numpy::Timedelta td_hours(5, "h"); // 5 hours
21164 std::cout << " Timedelta(5, 'h') = " << td_hours.total_hours() << " hours" << std::endl;
21165 if (std::abs(td_hours.total_hours() - 5.0) > 0.0001) {
21166 std::cout << "[FAIL] np_test_timedelta_constructors: Timedelta(5, 'h') expected 5 hours, got "
21167 << td_hours.total_hours() << std::endl;
21168 errors++;
21169 }
21170
21171 numpy::Timedelta td_days(3, "D"); // 3 days
21172 std::cout << " Timedelta(3, 'D') = " << td_days.days() << " days" << std::endl;
21173 if (td_days.days() != 3) {
21174 std::cout << "[FAIL] np_test_timedelta_constructors: Timedelta(3, 'D') expected 3 days, got "
Timedelta (np_test_5_all.cpp:21164)
21154 if (std::abs(td.total_hours() - 2.0) > 0.0001) {
21155 std::cout << "[FAIL] np_test_timedelta_constructors: from chrono expected 2 hours, got "
21156 << td.total_hours() << std::endl;
21157 errors++;
21158 }
21159 }
21160
21161 // From value and unit (pandas-style) - int64_t version
21162 {
21163 numpy::Timedelta td_hours(5, "h"); // 5 hours
21164 std::cout << " Timedelta(5, 'h') = " << td_hours.total_hours() << " hours" << std::endl;
21165 if (std::abs(td_hours.total_hours() - 5.0) > 0.0001) {
21166 std::cout << "[FAIL] np_test_timedelta_constructors: Timedelta(5, 'h') expected 5 hours, got "
21167 << td_hours.total_hours() << std::endl;
21168 errors++;
21169 }
21170
21171 numpy::Timedelta td_days(3, "D"); // 3 days
21172 std::cout << " Timedelta(3, 'D') = " << td_days.days() << " days" << std::endl;
21173 if (td_days.days() != 3) {
21174 std::cout << "[FAIL] np_test_timedelta_constructors: Timedelta(3, 'D') expected 3 days, got "
Timedelta (np_test_5_all.cpp:21164)
21154 if (std::abs(td.total_hours() - 2.0) > 0.0001) {
21155 std::cout << "[FAIL] np_test_timedelta_constructors: from chrono expected 2 hours, got "
21156 << td.total_hours() << std::endl;
21157 errors++;
21158 }
21159 }
21160
21161 // From value and unit (pandas-style) - int64_t version
21162 {
21163 numpy::Timedelta td_hours(5, "h"); // 5 hours
21164 std::cout << " Timedelta(5, 'h') = " << td_hours.total_hours() << " hours" << std::endl;
21165 if (std::abs(td_hours.total_hours() - 5.0) > 0.0001) {
21166 std::cout << "[FAIL] np_test_timedelta_constructors: Timedelta(5, 'h') expected 5 hours, got "
21167 << td_hours.total_hours() << std::endl;
21168 errors++;
21169 }
21170
21171 numpy::Timedelta td_days(3, "D"); // 3 days
21172 std::cout << " Timedelta(3, 'D') = " << td_days.days() << " days" << std::endl;
21173 if (td_days.days() != 3) {
21174 std::cout << "[FAIL] np_test_timedelta_constructors: Timedelta(3, 'D') expected 3 days, got "
from_value (np_test_5_all.cpp:21232)
21222 std::cout << " Timedelta(2, 'W') = " << td_week.days() << " days" << std::endl;
21223 if (td_week.days() != 14) {
21224 std::cout << "[FAIL] np_test_timedelta_constructors: Timedelta(2, 'W') expected 14 days, got "
21225 << td_week.days() << std::endl;
21226 errors++;
21227 }
21228 }
21229
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) {
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
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>())";
minutes (np_test_5_all.cpp:21131)
21121 if (td.value() != expected) {
21122 std::cout << "[FAIL] np_test_timedelta_constructors: from components expected "
21123 << expected << ", got " << td.value() << std::endl;
21124 errors++;
21125 }
21126 }
21127
21128 // From string "1 days 02:30:00"
21129 {
21130 numpy::Timedelta td("1 days 02:30:00");
21131 if (td.days() != 1 || td.hours() != 2 || td.minutes() != 30) {
21132 std::cout << "[FAIL] np_test_timedelta_constructors: from string '1 days 02:30:00' "
21133 << "got days=" << td.days() << " hours=" << td.hours()
21134 << " minutes=" << td.minutes() << std::endl;
21135 errors++;
21136 }
21137 }
21138
21139 // From timedelta64
21140 {
21141 numpy::timedelta64 td64(3600, numpy::DateTimeUnit::Second); // 1 hour
nanoseconds (np_test_5_all.cpp:21346)
21336 if (td.milliseconds() != 100) {
21337 std::cout << "[FAIL] np_test_timedelta_components: milliseconds() expected 100, got "
21338 << td.milliseconds() << std::endl;
21339 errors++;
21340 }
21341 if (td.microseconds() != 200) {
21342 std::cout << "[FAIL] np_test_timedelta_components: microseconds() expected 200, got "
21343 << td.microseconds() << std::endl;
21344 errors++;
21345 }
21346 if (td.nanoseconds() != 300) {
21347 std::cout << "[FAIL] np_test_timedelta_components: nanoseconds() expected 300, got "
21348 << td.nanoseconds() << std::endl;
21349 errors++;
21350 }
21351 }
21352
21353 // Components alias
21354 {
21355 numpy::Timedelta td(5, 0, 0);
21356 if (td.components_days() != 5) {
abs (np_test_1_all.cpp:101)
91 if (arr.getShape().size() == 1 && arr.getShape()[0] <= 10) {
92 for (size_t i = 0; i < arr.getShape()[0]; ++i) {
93 // std::cout << static_cast<int64_t>(arr.getElementAt({i})) << " ";
94 }
95 }
96 // std::cout << std::endl;
97 }
98
99 // Helper function for core array extensions tests
100 bool isApproxEqualExt(double a, double b, double tolerance = 1e-10) {
101 return std::abs(a - b) < tolerance;
102 }
103}
104
105void f_nothing() {
106 // This function does nothing and tests nothing
107 // It's a placeholder test function
108}
109
110// ------ merging np_test_advanced_indexing.cpp -- number of functions merged=16 --------------------------------
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);
to_chrono (np_test_5_all.cpp:21474)
21464 int64_t nanos = td.to_numpy();
21465 if (nanos != numpy::Timedelta::NANOS_PER_DAY) {
21466 std::cout << "[FAIL] np_test_timedelta_conversions: to_numpy expected "
21467 << numpy::Timedelta::NANOS_PER_DAY << ", got " << nanos << std::endl;
21468 errors++;
21469 }
21470 }
21471
21472 // to_chrono
21473 {
21474 std::chrono::nanoseconds dur = td.to_chrono();
21475 if (dur.count() != numpy::Timedelta::NANOS_PER_DAY) {
21476 std::cout << "[FAIL] np_test_timedelta_conversions: to_chrono expected "
21477 << numpy::Timedelta::NANOS_PER_DAY << ", got " << dur.count() << std::endl;
21478 errors++;
21479 }
21480 }
21481
21482 // to_chrono_duration
21483 {
21484 auto hrs = td.to_chrono_duration<std::chrono::hours>();
to_chrono_duration (np_test_5_all.cpp:21484)
21474 std::chrono::nanoseconds dur = td.to_chrono();
21475 if (dur.count() != numpy::Timedelta::NANOS_PER_DAY) {
21476 std::cout << "[FAIL] np_test_timedelta_conversions: to_chrono expected "
21477 << numpy::Timedelta::NANOS_PER_DAY << ", got " << dur.count() << std::endl;
21478 errors++;
21479 }
21480 }
21481
21482 // to_chrono_duration
21483 {
21484 auto hrs = td.to_chrono_duration<std::chrono::hours>();
21485 if (hrs.count() != 24) {
21486 std::cout << "[FAIL] np_test_timedelta_conversions: to_chrono_duration<hours> expected 24, got "
21487 << hrs.count() << std::endl;
21488 errors++;
21489 }
21490 }
21491
21492 if (errors == 0) {
21493 std::cout << "np_test_timedelta_conversions -> tests passed" << std::endl;
21494 }
to_numpy (np_test_5_all.cpp:21464)
21454 numpy::timedelta64 td64 = td.to_timedelta64(numpy::DateTimeUnit::Hour);
21455 if (td64.getValue() != 24) {
21456 std::cout << "[FAIL] np_test_timedelta_conversions: to_timedelta64(Hour) expected 24, got "
21457 << td64.getValue() << std::endl;
21458 errors++;
21459 }
21460 }
21461
21462 // to_numpy
21463 {
21464 int64_t nanos = td.to_numpy();
21465 if (nanos != numpy::Timedelta::NANOS_PER_DAY) {
21466 std::cout << "[FAIL] np_test_timedelta_conversions: to_numpy expected "
21467 << numpy::Timedelta::NANOS_PER_DAY << ", got " << nanos << std::endl;
21468 errors++;
21469 }
21470 }
21471
21472 // to_chrono
21473 {
21474 std::chrono::nanoseconds dur = td.to_chrono();
to_timedelta64 (np_test_5_all.cpp:21444)
21434// =============================================================================
21435// Test 5: Conversion Methods
21436// =============================================================================
21437int np_test_timedelta_conversions() {
21438 int errors = 0;
21439
21440 numpy::Timedelta td(1, 0, 0); // 1 day
21441
21442 // to_timedelta64 with Day unit
21443 {
21444 numpy::timedelta64 td64 = td.to_timedelta64(numpy::DateTimeUnit::Day);
21445 if (td64.getValue() != 1) {
21446 std::cout << "[FAIL] np_test_timedelta_conversions: to_timedelta64(Day) expected 1, got "
21447 << td64.getValue() << std::endl;
21448 errors++;
21449 }
21450 }
21451
21452 // to_timedelta64 with Hour unit
21453 {
21454 numpy::timedelta64 td64 = td.to_timedelta64(numpy::DateTimeUnit::Hour);
NaT (np_test_1_all.cpp:7020)
7010 // std::cout << april_2024.getElementAt({i}).toString() << " ";
7011 }
7012 // std::cout << std::endl;
7013
7014 std::cout << " -> tests passed" << std::endl;
7015}
7016
7017void testNaTHandlingDateTime() {
7018 std::cout << "========= testNaTHandlingDateTime =======================";
7019
7020 datetime64 nat_dt = datetime64::NaT();
7021 timedelta64 nat_td = timedelta64::NaT();
7022
7023 // std::cout << "NaT datetime: " << nat_dt.toString() << std::endl;
7024 // std::cout << "NaT timedelta: " << nat_td.toString() << std::endl;
7025
7026 datetime64 valid_date("2024-01-01");
7027
7028 // NaT arithmetic
7029 datetime64 result1 = valid_date + nat_td;
7030 datetime64 result2 = nat_dt + timedelta64(1, DateTimeUnit::Day);
as_unit (np_test_5_all.cpp:22957)
22947 }
22948
22949 // Replace multiple
22950 numpy::Timestamp multi = ts.replace(2025, 7, 20, 14);
22951 if (multi.year() != 2025 || multi.month() != 7 || multi.day() != 20 || multi.hour() != 14) {
22952 pass = false;
22953 fail_msg = "Replace multiple values failed";
22954 }
22955
22956 // as_unit
22957 numpy::Timestamp as_unit_ts = ts.as_unit("ns");
22958 if (as_unit_ts.isNaT()) {
22959 pass = false;
22960 fail_msg = "as_unit should not return NaT";
22961 }
22962
22963 if (!pass) {
22964 std::cout << " [FAIL] : in np_test_timestamp_replace() : " << fail_msg;
22965 throw std::runtime_error("np_test_timestamp_replace failed: " + fail_msg);
22966 }
components_days (np_test_5_all.cpp:21356)
21346 if (td.nanoseconds() != 300) {
21347 std::cout << "[FAIL] np_test_timedelta_components: nanoseconds() expected 300, got "
21348 << td.nanoseconds() << std::endl;
21349 errors++;
21350 }
21351 }
21352
21353 // Components alias
21354 {
21355 numpy::Timedelta td(5, 0, 0);
21356 if (td.components_days() != 5) {
21357 std::cout << "[FAIL] np_test_timedelta_components: components_days() expected 5, got "
21358 << td.components_days() << std::endl;
21359 errors++;
21360 }
21361 }
21362
21363 if (errors == 0) {
21364 std::cout << "np_test_timedelta_components -> tests passed" << std::endl;
21365 }
21366 return errors;
components_str (np_test_5_all.cpp:21550)
21540 if (iso.find("P1D") == std::string::npos) {
21541 std::cout << "[FAIL] np_test_timedelta_strings: isoformat expected 'P1D...', got '"
21542 << iso << "'" << std::endl;
21543 errors++;
21544 }
21545 }
21546
21547 // components_str
21548 {
21549 numpy::Timedelta td(1, 2, 30, 45, 100, 200, 300);
21550 std::string comp = td.components_str();
21551 if (comp.find("days=1") == std::string::npos) {
21552 std::cout << "[FAIL] np_test_timedelta_strings: components_str missing days=1, got '"
21553 << comp << "'" << std::endl;
21554 errors++;
21555 }
21556 }
21557
21558 if (errors == 0) {
21559 std::cout << "np_test_timedelta_strings -> tests passed" << std::endl;
21560 }
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 }
delta (np_test_5_all.cpp:21419)
21409 // total_days
21410 {
21411 double days = td.total_days();
21412 if (std::abs(days - 1.5) > 0.0001) {
21413 std::cout << "[FAIL] np_test_timedelta_total_conversions: total_days expected 1.5, got "
21414 << days << std::endl;
21415 errors++;
21416 }
21417 }
21418
21419 // value() and delta()
21420 {
21421 if (td.value() != td.delta()) {
21422 std::cout << "[FAIL] np_test_timedelta_total_conversions: value() != delta()"
21423 << std::endl;
21424 errors++;
21425 }
21426 }
21427
21428 if (errors == 0) {
21429 std::cout << "np_test_timedelta_total_conversions -> tests passed" << std::endl;
hours (np_test_5_all.cpp:21131)
21121 if (td.value() != expected) {
21122 std::cout << "[FAIL] np_test_timedelta_constructors: from components expected "
21123 << expected << ", got " << td.value() << std::endl;
21124 errors++;
21125 }
21126 }
21127
21128 // From string "1 days 02:30:00"
21129 {
21130 numpy::Timedelta td("1 days 02:30:00");
21131 if (td.days() != 1 || td.hours() != 2 || td.minutes() != 30) {
21132 std::cout << "[FAIL] np_test_timedelta_constructors: from string '1 days 02:30:00' "
21133 << "got days=" << td.days() << " hours=" << td.hours()
21134 << " minutes=" << td.minutes() << std::endl;
21135 errors++;
21136 }
21137 }
21138
21139 // From timedelta64
21140 {
21141 numpy::timedelta64 td64(3600, numpy::DateTimeUnit::Second); // 1 hour
isNaT (np_test_1_all.cpp:6821)
6811 std::cout << " -> tests passed" << std::endl;
6812}
6813
6814// DateTime Functions (merged from np_test_datetime.cpp)
6815void testDatetime64CreationDateTime() {
6816 std::cout << "========= testDatetime64CreationDateTime =======================";
6817
6818 // Test default constructor
6819 datetime64 default_dt;
6820 if (!(default_dt.isNaT())) {
6821 std::string description = std::string("testDatetime64CreationDateTime():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(default_dt.isNaT())";
6822 std::cout << std::string("[FAIL] ") + description << std::endl;
6823 throw std::runtime_error(description);
6824 }
6825 // std::cout << "Default datetime64 is NaT: " << default_dt.toString() << std::endl;
6826
6827 // Test value constructor
6828 datetime64 epoch_day(0, DateTimeUnit::Day);
6829 // std::cout << "Epoch day: " << epoch_day.toString() << std::endl;
isoformat (np_test_5_all.cpp:21539)
21529 if (nat.toString() != "NaT") {
21530 std::cout << "[FAIL] np_test_timedelta_strings: toString(NaT) expected 'NaT', got '"
21531 << nat.toString() << "'" << std::endl;
21532 errors++;
21533 }
21534 }
21535
21536 // isoformat
21537 {
21538 numpy::Timedelta td(1, 2, 30);
21539 std::string iso = td.isoformat();
21540 if (iso.find("P1D") == std::string::npos) {
21541 std::cout << "[FAIL] np_test_timedelta_strings: isoformat expected 'P1D...', got '"
21542 << iso << "'" << std::endl;
21543 errors++;
21544 }
21545 }
21546
21547 // components_str
21548 {
21549 numpy::Timedelta td(1, 2, 30, 45, 100, 200, 300);
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 }
milliseconds (np_test_1_all.cpp:6046)
6036// ------ merging np_test_benchmark_sorting.cpp -- number of functions merged= 11 --------------------------------
6037
6038void test_timer_accuracyBenchmarkSorting() {
6039 std::cout << "========= test_timer_accuracy =======================";
6040
6041 Timer timer;
6042 timer.start();
6043
6044 // Sleep for a known duration (10ms)
6045 std::this_thread::sleep_for(std::chrono::milliseconds(10));
6046
6047 double elapsed = timer.elapsed_milliseconds();
6048
6049 // Timer should be approximately 10ms (allow 5ms tolerance for system variance)
6050 if (!(elapsed >= 3.0 && elapsed <= 105.0)) {
6051 std::string description = std::string("test_timer_accuracyBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(elapsed >= 3.0 && elapsed <= 105.0)";
6052 std::cout << std::string("[FAIL] ") + description << std::endl;
6053 throw std::runtime_error(description);
6054 }
6055 // std::cout << "Timer accuracy test passed. Elapsed: " << elapsed << " ms" << std::endl;
resolution (np_test_5_all.cpp:21291)
21281 // max()
21282 {
21283 numpy::Timedelta max_td = numpy::Timedelta::max();
21284 if (max_td.value() != INT64_MAX) {
21285 std::cout << "[FAIL] np_test_timedelta_static_factories: max() expected INT64_MAX"
21286 << std::endl;
21287 errors++;
21288 }
21289 }
21290
21291 // resolution()
21292 {
21293 numpy::Timedelta res = numpy::Timedelta::resolution();
21294 if (res.value() != 1) {
21295 std::cout << "[FAIL] np_test_timedelta_static_factories: resolution() expected 1 ns"
21296 << std::endl;
21297 errors++;
21298 }
21299 }
21300
21301 if (errors == 0) {
seconds (np_test_5_all.cpp:21331)
21321 if (td.hours() != 2) {
21322 std::cout << "[FAIL] np_test_timedelta_components: hours() expected 2, got "
21323 << td.hours() << std::endl;
21324 errors++;
21325 }
21326 if (td.minutes() != 30) {
21327 std::cout << "[FAIL] np_test_timedelta_components: minutes() expected 30, got "
21328 << td.minutes() << std::endl;
21329 errors++;
21330 }
21331 if (td.seconds() != 45) {
21332 std::cout << "[FAIL] np_test_timedelta_components: seconds() expected 45, got "
21333 << td.seconds() << std::endl;
21334 errors++;
21335 }
21336 if (td.milliseconds() != 100) {
21337 std::cout << "[FAIL] np_test_timedelta_components: milliseconds() expected 100, got "
21338 << td.milliseconds() << std::endl;
21339 errors++;
21340 }
21341 if (td.microseconds() != 200) {
toString (np_test_1_all.cpp:6826)
6816void testDatetime64CreationDateTime() {
6817 std::cout << "========= testDatetime64CreationDateTime =======================";
6818
6819 // Test default constructor
6820 datetime64 default_dt;
6821 if (!(default_dt.isNaT())) {
6822 std::string description = std::string("testDatetime64CreationDateTime():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(default_dt.isNaT())";
6823 std::cout << std::string("[FAIL] ") + description << std::endl;
6824 throw std::runtime_error(description);
6825 }
6826 // std::cout << "Default datetime64 is NaT: " << default_dt.toString() << std::endl;
6827
6828 // Test value constructor
6829 datetime64 epoch_day(0, DateTimeUnit::Day);
6830 // std::cout << "Epoch day: " << epoch_day.toString() << std::endl;
6831
6832 // Test string constructor
6833 datetime64 date_from_string("2024-01-15");
6834 // std::cout << "Date from string '2024-01-15': " << date_from_string.toString() << std::endl;
6835
6836 datetime64 datetime_from_string("2024-01-15T10:30:45");
total_days (np_test_5_all.cpp:21411)
21401 double hrs = td.total_hours();
21402 if (std::abs(hrs - 36.0) > 0.0001) {
21403 std::cout << "[FAIL] np_test_timedelta_total_conversions: total_hours expected 36, got "
21404 << hrs << std::endl;
21405 errors++;
21406 }
21407 }
21408
21409 // total_days
21410 {
21411 double days = td.total_days();
21412 if (std::abs(days - 1.5) > 0.0001) {
21413 std::cout << "[FAIL] np_test_timedelta_total_conversions: total_days expected 1.5, got "
21414 << days << std::endl;
21415 errors++;
21416 }
21417 }
21418
21419 // value() and delta()
21420 {
21421 if (td.value() != td.delta()) {
total_hours (np_test_5_all.cpp:21143)
21133 << "got days=" << td.days() << " hours=" << td.hours()
21134 << " minutes=" << td.minutes() << std::endl;
21135 errors++;
21136 }
21137 }
21138
21139 // From timedelta64
21140 {
21141 numpy::timedelta64 td64(3600, numpy::DateTimeUnit::Second); // 1 hour
21142 numpy::Timedelta td(td64);
21143 if (std::abs(td.total_hours() - 1.0) > 0.0001) {
21144 std::cout << "[FAIL] np_test_timedelta_constructors: from timedelta64 expected 1 hour, got "
21145 << td.total_hours() << std::endl;
21146 errors++;
21147 }
21148 }
21149
21150 // From std::chrono::duration
21151 {
21152 std::chrono::hours h(2);
21153 numpy::Timedelta td(h);
total_minutes (np_test_5_all.cpp:21180)
21170 numpy::Timedelta td_days(3, "D"); // 3 days
21171 std::cout << " Timedelta(3, 'D') = " << td_days.days() << " days" << std::endl;
21172 if (td_days.days() != 3) {
21173 std::cout << "[FAIL] np_test_timedelta_constructors: Timedelta(3, 'D') expected 3 days, got "
21174 << td_days.days() << std::endl;
21175 errors++;
21176 }
21177
21178 numpy::Timedelta td_min(90, "min"); // 90 minutes = 1.5 hours
21179 std::cout << " Timedelta(90, 'min') = " << td_min.total_minutes() << " minutes" << std::endl;
21180 if (std::abs(td_min.total_minutes() - 90.0) > 0.0001) {
21181 std::cout << "[FAIL] np_test_timedelta_constructors: Timedelta(90, 'min') expected 90 minutes, got "
21182 << td_min.total_minutes() << std::endl;
21183 errors++;
21184 }
21185
21186 numpy::Timedelta td_sec(30, "s"); // 30 seconds
21187 std::cout << " Timedelta(30, 's') = " << td_sec.total_seconds() << " seconds" << std::endl;
21188 if (std::abs(td_sec.total_seconds() - 30.0) > 0.0001) {
21189 std::cout << "[FAIL] np_test_timedelta_constructors: Timedelta(30, 's') expected 30 seconds, got "
total_seconds (np_test_5_all.cpp:21188)
21178 numpy::Timedelta td_min(90, "min"); // 90 minutes = 1.5 hours
21179 std::cout << " Timedelta(90, 'min') = " << td_min.total_minutes() << " minutes" << std::endl;
21180 if (std::abs(td_min.total_minutes() - 90.0) > 0.0001) {
21181 std::cout << "[FAIL] np_test_timedelta_constructors: Timedelta(90, 'min') expected 90 minutes, got "
21182 << td_min.total_minutes() << std::endl;
21183 errors++;
21184 }
21185
21186 numpy::Timedelta td_sec(30, "s"); // 30 seconds
21187 std::cout << " Timedelta(30, 's') = " << td_sec.total_seconds() << " seconds" << std::endl;
21188 if (std::abs(td_sec.total_seconds() - 30.0) > 0.0001) {
21189 std::cout << "[FAIL] np_test_timedelta_constructors: Timedelta(30, 's') expected 30 seconds, got "
21190 << td_sec.total_seconds() << std::endl;
21191 errors++;
21192 }
21193
21194 numpy::Timedelta td_ms(500, "ms"); // 500 milliseconds
21195 std::cout << " Timedelta(500, 'ms') = " << td_ms.value() << " nanoseconds" << std::endl;
21196 int64_t expected_ms = 500 * numpy::Timedelta::NANOS_PER_MILLISECOND;
21197 if (td_ms.value() != expected_ms) {
value (np_test_1_all.cpp:7786)
7776}
7777
7778void testFloat16PrecisionFloat16() {
7779 std::cout << "========= testFloat16PrecisionFloat16 =======================";
7780
7781 // Test precision limits
7782 float16 small_val(0.0001f);
7783 float16 large_val(30000.0f);
7784 float16 very_small(1e-8f); // This might underflow to zero
7785
7786 // std::cout << "Small value (0.0001): " << small_val << std::endl;
7787 // std::cout << "Large value (30000): " << large_val << std::endl;
7788 // std::cout << "Very small value (1e-8): " << very_small << std::endl;
7789
7790 // Test conversion accuracy
7791 float original = 1.234f;
7792 float16 converted(original);
7793 float back_converted = static_cast<float>(converted);
7794
7795 // std::cout << "Original float: " << std::setprecision(10) << original << std::endl;
7796 // std::cout << "Float16 version: " << converted << std::endl;