LCOV - code coverage report
Current view: top level - pairinteraction/unit_test - cache_test.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 24 24 100.0 %
Date: 2024-04-29 00:41:50 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2017 Sebastian Weber, Henri Menke. All rights reserved.
       3             :  *
       4             :  * This file is part of the pairinteraction library.
       5             :  *
       6             :  * The pairinteraction library is free software: you can redistribute it and/or modify
       7             :  * it under the terms of the GNU Lesser General Public License as published by
       8             :  * the Free Software Foundation, either version 3 of the License, or
       9             :  * (at your option) any later version.
      10             :  *
      11             :  * The pairinteraction library is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :  * GNU Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public License
      17             :  * along with the pairinteraction library. If not, see <http://www.gnu.org/licenses/>.
      18             :  */
      19             : 
      20             : #include "Cache.hpp"
      21             : #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
      22             : #include <doctest/doctest.h>
      23             : 
      24             : #include <optional>
      25             : #include <thread>
      26             : #include <vector>
      27             : 
      28           1 : TEST_CASE("cache_test") // NOLINT
      29             : {
      30           2 :     Cache<std::string, int> cache;
      31             : 
      32           1 :     CHECK_NOTHROW(cache.save("Hello world!", 1));
      33             : 
      34           1 :     std::optional<int> oe;
      35           2 :     CHECK_NOTHROW(oe = cache.restore("Hello world!"));
      36           1 :     CHECK(oe.value() == 1);
      37             : 
      38           1 :     CHECK_NOTHROW(cache.clear());
      39           1 : }
      40             : 
      41           1 : TEST_CASE("smash_test") // NOLINT
      42             : {
      43           2 :     Cache<std::string, int> cache;
      44             : 
      45           1 :     CHECK_NOTHROW(cache.save("Hello world!", 1));
      46           3 :     CHECK_THROWS_AS(cache.save("Hello world!", 2), std::runtime_error);
      47           1 : }
      48             : 
      49           1 : TEST_CASE("parallel_cache_test") // NOLINT
      50             : {
      51           2 :     Cache<int, std::string> cache;
      52             : 
      53           2 :     std::vector<std::thread> threads(10);
      54             : 
      55          11 :     for (std::size_t i = 0; i < 10; ++i) {
      56          10 :         threads[i] =
      57          30 :             std::thread([&cache, i]() { cache.save(i, "Hello from thread " + std::to_string(i)); });
      58             :     }
      59             : 
      60          11 :     for (std::size_t i = 0; i < 10; ++i) {
      61          10 :         threads[i].join();
      62             :     }
      63             : 
      64          11 :     for (std::size_t i = 0; i < 10; ++i) {
      65          10 :         CHECK(cache.restore(i).value() == "Hello from thread " + std::to_string(i));
      66             :     }
      67           1 : }

Generated by: LCOV version 1.14