Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-27 07:24:14

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 // Project include(s)
0012 #include "detray/algebra/concepts.hpp"
0013 #include "detray/algebra/utils/approximately_equal.hpp"
0014 
0015 // Test include(s).
0016 #include "detray/test/framework/fixture_base.hpp"
0017 #include "detray/test/framework/types.hpp"
0018 
0019 // VecMem include(s).
0020 #include <vecmem/containers/vector.hpp>
0021 #include <vecmem/memory/memory_resource.hpp>
0022 
0023 // GoogleTest include(s).
0024 #include <gtest/gtest.h>
0025 
0026 // System include(s).
0027 #include <cmath>
0028 #include <memory>
0029 #include <type_traits>
0030 
0031 namespace detray::test {
0032 
0033 /// Data fixture for device test cases
0034 template <typename R>
0035   requires std::is_default_constructible_v<R>
0036 class device_fixture : public fixture_base<> {
0037  public:
0038   /// Constructor, setting up the vecmem resource
0039   device_fixture(vecmem::memory_resource& mr) : m_resource(mr) {}
0040 
0041  protected:
0042   /// Set up the host and device results
0043   virtual void SetUp() override {
0044     m_output_host =
0045         std::make_unique<vecmem::vector<R>>(this->size(), &m_resource);
0046     m_output_device =
0047         std::make_unique<vecmem::vector<R>>(this->size(), &m_resource);
0048 
0049     for (std::size_t i = 0u; i < this->size(); ++i) {
0050       m_output_host->at(i) = {};
0051       m_output_device->at(i) = {};
0052     }
0053   }
0054 
0055   /// Function resetting the output after the test
0056   virtual void TearDown() override {
0057     m_output_host.reset();
0058     m_output_device.reset();
0059   }
0060 
0061   /// @returns the number of results
0062   constexpr std::size_t size() const { return m_size; }
0063 
0064   /// @returns access to the memory resource of the device test suite
0065   constexpr vecmem::memory_resource& resource() { return m_resource; }
0066 
0067   /// Compare the outputs, after the data processing is finished.
0068   void compareOutputs() const {
0069     for (std::size_t i = 0u; i < this->size(); ++i) {
0070       // Use the inbuilt comparator for algebra types
0071       if constexpr (detray::concepts::value<R> || detray::concepts::scalar<R> ||
0072                     detray::concepts::vector<R> ||
0073                     detray::concepts::matrix<R>) {
0074         EXPECT_TRUE(detray::algebra::approx_equal(
0075             m_output_host->at(i), m_output_device->at(i), this->tolerance()))
0076             << "error at i = " << i << "\nHOST:\n"
0077             << m_output_host->at(i) << "\nDEVICE:\n"
0078             << m_output_device->at(i);
0079 
0080       } else {
0081         EXPECT_EQ(m_output_host->at(i), m_output_device->at(i))
0082             << "error at i = " << i << "\nHOST:\n"
0083             << m_output_host->at(i) << "\nDEVICE:\n"
0084             << m_output_device->at(i);
0085       }
0086     }
0087   }
0088 
0089   /// Memory resource provided by the derived class
0090   vecmem::memory_resource& m_resource;
0091 
0092   /// Size for the tested arrays.
0093   static constexpr std::size_t m_size = 5000u;
0094 
0095   /// @name Outputs for the tests
0096   /// @{
0097   std::unique_ptr<vecmem::vector<R>> m_output_host;
0098   std::unique_ptr<vecmem::vector<R>> m_output_device;
0099   /// @}
0100 
0101 };  // class device_fixture
0102 
0103 }  // namespace detray::test