LCOV - code coverage report
Current view: top level - test - api.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 69 69
Test Date: 2026-04-03 02:26:39 Functions: 100.0 % 3 3

            Line data    Source code
       1              : #include "catch.hpp"
       2              : #include "Vector.h"
       3              : #include "api_vector.h"
       4              : #include <cstring>
       5              : 
       6            2 : static Vector loadVector(lua_State *L, int index) {
       7            2 :   Vector t;
       8              : 
       9              :   // x
      10            2 :   lua_pushnumber(L, 1);
      11            2 :   lua_gettable(L, index - 1);
      12            2 :   t.x = (float)lua_tonumber(L, -1);
      13            2 :   lua_pop(L, 1);
      14              : 
      15              :   // y
      16            2 :   lua_pushnumber(L, 2);
      17            2 :   lua_gettable(L, index - 1);
      18            2 :   t.y = (float)lua_tonumber(L, -1);
      19            2 :   lua_pop(L, 1);
      20              : 
      21              :   // z
      22            2 :   lua_pushnumber(L, 3);
      23            2 :   lua_gettable(L, index - 1);
      24            2 :   t.z = (float)lua_tonumber(L, -1);
      25            2 :   lua_pop(L, 1);
      26              : 
      27            2 :   return t;
      28              : }
      29              : 
      30            5 : SCENARIO( "Using Lua API", "[API]" ) {
      31            8 :   GIVEN( "A Lua state with the API loaded" ) {
      32            4 :     lua_State *L = lua_open();
      33            4 :     REQUIRE(L != NULL);
      34              : 
      35            4 :     luaL_openlibs(L);
      36            4 :     luaopen_krigVector(L);
      37              : 
      38            5 :     WHEN( "vector_normalize is called" ) {
      39            1 :       const char* buffer = "v = krig.vector.normalize({1,2,3})\n";
      40            2 :       luaL_loadbuffer(L, buffer, strlen(buffer), "tests") ||
      41            1 :       lua_pcall(L, 0, 0, 0);
      42            2 :       THEN( "It returns a normalized vector" ) {
      43            1 :         lua_getglobal(L, "v");
      44            1 :         REQUIRE(lua_istable(L, -1));
      45            1 :         Vector v = loadVector(L, -1);
      46            1 :         REQUIRE(v.x == Approx(0.26726f));
      47            1 :         REQUIRE(v.y == Approx(0.53452f));
      48            1 :         REQUIRE(v.z == Approx(0.80178f));
      49            1 :       }
      50            1 :     }
      51              : 
      52            5 :     WHEN( "vector_get_scalar is called" ) {
      53            1 :       const char* buffer = "v = krig.vector.scalar({1,2,3}, {4,5,6})\n";
      54            2 :       luaL_loadbuffer(L, buffer, strlen(buffer), "tests") ||
      55            1 :       lua_pcall(L, 0, 0, 0);
      56            2 :       THEN( "It returns scalar value between two vectors" ) {
      57            1 :         lua_getglobal(L, "v");
      58            1 :         REQUIRE(lua_isnumber(L, -1));
      59            1 :         REQUIRE((float)lua_tonumber(L, -1) == Approx(32.0f));
      60            1 :       }
      61            1 :     }
      62              : 
      63            5 :     WHEN( "vector_dot_product is called" ) {
      64            1 :       const char* buffer = "v = krig.vector.dot_product({1,2,3}, {4,5,6})\n";
      65            2 :       luaL_loadbuffer(L, buffer, strlen(buffer), "tests") ||
      66            1 :       lua_pcall(L, 0, 0, 0);
      67            2 :       THEN( "It returns dot product for the two vectors" ) {
      68            1 :         lua_getglobal(L, "v");
      69            1 :         REQUIRE(lua_isnumber(L, -1));
      70            1 :         REQUIRE((float)lua_tonumber(L, -1) == Approx(32.0f));
      71            1 :       }
      72            1 :     }
      73              : 
      74            5 :     WHEN( "vector_cross_product is called" ) {
      75            1 :       const char* buffer = "v = krig.vector.cross_product({1,2,3}, {4,5,6})\n";
      76            2 :       luaL_loadbuffer(L, buffer, strlen(buffer), "tests") ||
      77            1 :       lua_pcall(L, 0, 0, 0);
      78            2 :       THEN( "It returns cross product for the two vectors" ) {
      79            1 :         lua_getglobal(L, "v");
      80            1 :         REQUIRE(lua_istable(L, -1));
      81            1 :         Vector v = loadVector(L, -1);
      82            1 :         REQUIRE(v.x == Approx(-3.0f));
      83            1 :         REQUIRE(v.y == Approx(6.0f));
      84            1 :         REQUIRE(v.z == Approx(-3.0f));
      85            1 :       }
      86            1 :     }
      87            4 :   }
      88            4 : }
        

Generated by: LCOV version 2.4-0