#include "test_common.h" #include "./utils/utils.h" using utils::Vf; using utils::Vd; using utils::Vi; // ---------- helpers ---------- template static bool vec_equal_exact(const utils::Vector& a, const utils::Vector& b) { if (a.size() != b.size()) return false; for (std::uint64_t i=0;i [2,2,2] CHECK(v[0]==2.0 && v[1]==2.0, "inplace_sqrt"); } TEST_CASE(Vector_Dot_Sum_Norm) { utils::Vd a(3, 0.0), b(3, 0.0); a[0]=1.0; a[1]=2.0; a[2]=3.0; // a = [1,2,3] b[0]=4.0; b[1]=5.0; b[2]=6.0; // b = [4,5,6] double dot = a.dot(b); // 1*4 + 2*5 + 3*6 = 32 CHECK(std::fabs(dot - 32.0) < 1e-12, "dot"); double s = a.sum(); // 6 CHECK(std::fabs(s - 6.0) < 1e-12, "sum"); double n = a.norm(); // sqrt(14) CHECK(std::fabs(n - std::sqrt(14.0)) < 1e-12, "norm"); } TEST_CASE(Vector_Normalize_and_Throws) { utils::Vd v(3, 0.0); v[0]=3.0; v[1]=4.0; v[2]=0.0; // norm = 5 auto u = v.normalize(); // returns new vector CHECK(std::fabs(u.norm() - 1.0) < 1e-12, "normalize() unit length"); v.inplace_normalize(); CHECK(std::fabs(v.norm() - 1.0) < 1e-12, "inplace_normalize unit length"); utils::Vd z(3, 0.0); bool threw=false; try { z.inplace_normalize(); } catch(const std::runtime_error&) { threw=true; } CHECK(threw, "normalize should throw on zero vector"); } // Size mismatch throws (elementwise ops) TEST_CASE(Vector_Size_Mismatch_Throws) { utils::Vi a(3,1), b(4,2); bool threw=false; try { (void)a.dot(b); } catch(const std::runtime_error&) { threw=true; } CHECK(threw, "dot size mismatch should throw"); threw=false; try { a.inplace_add(b); } catch(const std::runtime_error&) { threw=true; } CHECK(threw, "add size mismatch should throw"); threw=false; try { a.inplace_subtract(b); } catch(const std::runtime_error&) { threw=true; } CHECK(threw, "subtract size mismatch should throw"); threw=false; try { a.inplace_multiply(b); } catch(const std::runtime_error&) { threw=true; } CHECK(threw, "multiply size mismatch should throw"); threw=false; try { a.inplace_divide(b); } catch(const std::runtime_error&) { threw=true; } CHECK(threw, "divide size mismatch should throw"); threw=false; try { a.inplace_power(b); } catch(const std::runtime_error&) { threw=true; } CHECK(threw, "power size mismatch should throw"); }