From 60c55ea21f927327e8dedb8b92711b4360158741 Mon Sep 17 00:00:00 2001
From: Millian Poquet <millian.poquet@irit.fr>
Date: Sun, 13 Nov 2022 18:22:12 +0100
Subject: [PATCH] c++ unit tests (gtest)

---
 default.nix                                |  1 +
 simgrid-simulator/meson.build              | 16 ++++++++++++
 simgrid-simulator/meson_options.txt        |  2 ++
 simgrid-simulator/test/flop_conversion.cpp | 29 ++++++++++++++++++++++
 4 files changed, 48 insertions(+)
 create mode 100644 simgrid-simulator/meson_options.txt
 create mode 100644 simgrid-simulator/test/flop_conversion.cpp

diff --git a/default.nix b/default.nix
index 6a5ee57..fa7de3d 100644
--- a/default.nix
+++ b/default.nix
@@ -56,6 +56,7 @@ let self = rec {
       pkg-config
       ninja
       zeromq
+      gtest.dev
     ];
   };
   client-cpp = pkgs.stdenv.mkDerivation {
diff --git a/simgrid-simulator/meson.build b/simgrid-simulator/meson.build
index 6de2734..c316e14 100644
--- a/simgrid-simulator/meson.build
+++ b/simgrid-simulator/meson.build
@@ -24,3 +24,19 @@ simulator = executable('simulator', [
   dependencies: deps,
   install: true
 )
+
+# unit tests with gtest
+if get_option('do_unit_tests')
+    gtest_dep = dependency('gtest_main', version: '>=1.8.0', required: true)
+    test_incdir = include_directories('test', 'src')
+    test_src = [
+        'test/flop_conversion.cpp',
+        'src/compute.cpp', 'src/compute.hpp'
+    ]
+    unittest = executable('simulator-unit-tester',
+        test_src,
+        dependencies: [gtest_dep],
+        include_directories: [test_incdir]
+    )
+    test('unittest', unittest)
+endif
diff --git a/simgrid-simulator/meson_options.txt b/simgrid-simulator/meson_options.txt
new file mode 100644
index 0000000..618662e
--- /dev/null
+++ b/simgrid-simulator/meson_options.txt
@@ -0,0 +1,2 @@
+option('do_unit_tests', type : 'boolean', value : false,
+    description : 'Enable unit tests (requires gtest)')
diff --git a/simgrid-simulator/test/flop_conversion.cpp b/simgrid-simulator/test/flop_conversion.cpp
new file mode 100644
index 0000000..50121f4
--- /dev/null
+++ b/simgrid-simulator/test/flop_conversion.cpp
@@ -0,0 +1,29 @@
+#include <gtest/gtest.h>
+
+#include "compute.hpp"
+
+void test_wrapper_flop_conversion(float desired, float actual)
+{
+    float got = desired_flops_to_simulated_flops(desired);
+    EXPECT_FLOAT_EQ(got, actual);
+}
+
+TEST(flop_conversion, small)
+{
+    test_wrapper_flop_conversion(1.0f, 1.0f);
+    test_wrapper_flop_conversion(5.0f, 5.0f);
+    test_wrapper_flop_conversion(10.0f, 10.0f);
+    test_wrapper_flop_conversion(100.0f, 100.0f);
+}
+
+TEST(flop_conversion, big)
+{
+    test_wrapper_flop_conversion(1e6, 1e6);
+    test_wrapper_flop_conversion(1e9, 1e9);
+    test_wrapper_flop_conversion(1e18, 1e18);
+}
+
+TEST(flop_conversion, wrong)
+{
+    test_wrapper_flop_conversion(1e26, 1e27);
+}
-- 
GitLab