Select the types of activity you want to include in your feed.
vec3: add unit test suite
Adds the missing test/ directory with a small alcotest suite covering zero, add, sub, scale, dot, cross, length, normalize, negate, distance, equal, and pp.
···11+(*---------------------------------------------------------------------------
22+ Copyright (c) 2026 Thomas Gazagnaire. All rights reserved.
33+ SPDX-License-Identifier: ISC
44+ ---------------------------------------------------------------------------*)
55+66+(** Tests for {!Vec3} basic arithmetic and norms. *)
77+88+let v = Vec3.v
99+1010+let approx ?(eps = 1e-12) msg expected actual =
1111+ if Float.abs (expected -. actual) > eps then
1212+ Alcotest.failf "%s: expected %.14g, got %.14g" msg expected actual
1313+1414+let test_zero () =
1515+ let z = Vec3.zero in
1616+ approx "zero.x" 0.0 z.x;
1717+ approx "zero.y" 0.0 z.y;
1818+ approx "zero.z" 0.0 z.z
1919+2020+let test_add () =
2121+ let r = Vec3.add (v 1. 2. 3.) (v 4. 5. 6.) in
2222+ approx "add.x" 5.0 r.x;
2323+ approx "add.y" 7.0 r.y;
2424+ approx "add.z" 9.0 r.z
2525+2626+let test_sub () =
2727+ let r = Vec3.sub (v 4. 5. 6.) (v 1. 2. 3.) in
2828+ approx "sub.x" 3.0 r.x;
2929+ approx "sub.y" 3.0 r.y;
3030+ approx "sub.z" 3.0 r.z
3131+3232+let test_scale () =
3333+ let r = Vec3.scale 2.0 (v 1. 2. 3.) in
3434+ approx "scale.x" 2.0 r.x;
3535+ approx "scale.y" 4.0 r.y;
3636+ approx "scale.z" 6.0 r.z
3737+3838+let test_dot () = approx "dot" 32.0 (Vec3.dot (v 1. 2. 3.) (v 4. 5. 6.))
3939+4040+let test_cross () =
4141+ (* x cross y = z *)
4242+ let r = Vec3.cross (v 1. 0. 0.) (v 0. 1. 0.) in
4343+ approx "cross.x" 0.0 r.x;
4444+ approx "cross.y" 0.0 r.y;
4545+ approx "cross.z" 1.0 r.z
4646+4747+let test_length () =
4848+ approx "length 3-4-0" 5.0 (Vec3.length (v 3. 4. 0.));
4949+ approx "length zero" 0.0 (Vec3.length Vec3.zero)
5050+5151+let test_normalize () =
5252+ let r = Vec3.normalize (v 3. 4. 0.) in
5353+ approx "normalize.x" 0.6 r.x;
5454+ approx "normalize.y" 0.8 r.y;
5555+ approx "normalize.z" 0.0 r.z;
5656+ let z = Vec3.normalize (v 1e-15 0. 0.) in
5757+ approx "normalize tiny -> zero" 0.0 (Vec3.length z)
5858+5959+let test_negate () =
6060+ let r = Vec3.negate (v 1. (-2.) 3.) in
6161+ approx "negate.x" (-1.0) r.x;
6262+ approx "negate.y" 2.0 r.y;
6363+ approx "negate.z" (-3.0) r.z
6464+6565+let test_distance () =
6666+ approx "distance" 5.0 (Vec3.distance (v 1. 1. 0.) (v 4. 5. 0.))
6767+6868+let test_equal () =
6969+ Alcotest.(check bool)
7070+ "exact equal" true
7171+ (Vec3.equal (v 1. 2. 3.) (v 1. 2. 3.));
7272+ Alcotest.(check bool)
7373+ "eps equal" true
7474+ (Vec3.equal ~eps:1e-6 (v 1.0 2.0 3.0) (v 1.0000001 2.0 3.0));
7575+ Alcotest.(check bool) "not equal" false (Vec3.equal (v 1. 2. 3.) (v 1. 2. 4.))
7676+7777+let test_pp () =
7878+ let s = Fmt.str "%a" Vec3.pp (v 1. 2. 3.) in
7979+ Alcotest.(check bool)
8080+ "pp non-empty contains coords" true
8181+ (String.length s > 0
8282+ && String.contains s '1' && String.contains s '2' && String.contains s '3')
8383+8484+let suite =
8585+ ( "vec3",
8686+ [
8787+ Alcotest.test_case "zero" `Quick test_zero;
8888+ Alcotest.test_case "add" `Quick test_add;
8989+ Alcotest.test_case "sub" `Quick test_sub;
9090+ Alcotest.test_case "scale" `Quick test_scale;
9191+ Alcotest.test_case "dot" `Quick test_dot;
9292+ Alcotest.test_case "cross x*y=z" `Quick test_cross;
9393+ Alcotest.test_case "length" `Quick test_length;
9494+ Alcotest.test_case "normalize" `Quick test_normalize;
9595+ Alcotest.test_case "negate" `Quick test_negate;
9696+ Alcotest.test_case "distance" `Quick test_distance;
9797+ Alcotest.test_case "equal" `Quick test_equal;
9898+ Alcotest.test_case "pp" `Quick test_pp;
9999+ ] )
+4
test/test_vec3.mli
···11+(** Tests for the [vec3] library. *)
22+33+val suite : string * unit Alcotest.test_case list
44+(** [suite] is the alcotest suite for vec3 arithmetic and norms. *)