| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include <catch2/catch_all.hpp> |
| 5 | |
| 6 | #include "core/info/cpuinfo.h" |
| 7 | |
| 8 | namespace Tests::CPUInfo { |
| 9 | |
| 10 | class ProviderStub : public ICPUInfo::IProvider |
| 11 | { |
| 12 | public: |
| 13 | std::vector<std::pair<std::string, std::string>> |
| 14 | provideInfo(int, std::vector<ICPUInfo::ExecutionUnit> const &) override |
| 15 | { |
| 16 | std::vector<std::pair<std::string, std::string>> info; |
| 17 | info.emplace_back("info_key", "info"); |
| 18 | return info; |
| 19 | } |
| 20 | |
| 21 | std::vector<std::string> |
| 22 | provideCapabilities(int, std::vector<ICPUInfo::ExecutionUnit> const &) override |
| 23 | { |
| 24 | std::vector<std::string> cap; |
| 25 | cap.emplace_back("capability"); |
| 26 | return cap; |
| 27 | } |
| 28 | }; |
| 29 | |
| 30 | TEST_CASE("CPUInfo tests", "[Info][CPUInfo]") |
| 31 | { |
| 32 | int physicalId{0}; |
| 33 | ::CPUInfo ts(physicalId, {{0, 0, "/proc/cpu0"}}); |
| 34 | |
| 35 | SECTION("Has CPU physical id") |
| 36 | { |
| 37 | REQUIRE(ts.physicalId() == physicalId); |
| 38 | } |
| 39 | |
| 40 | SECTION("Has execution units") |
| 41 | { |
| 42 | REQUIRE_FALSE(ts.executionUnits().empty()); |
| 43 | |
| 44 | auto &unit = ts.executionUnits().front(); |
| 45 | REQUIRE(unit.cpuId == 0); |
| 46 | REQUIRE(unit.coreId == 0); |
| 47 | REQUIRE(unit.sysPath == "/proc/cpu0"); |
| 48 | } |
| 49 | |
| 50 | SECTION("CPU info and capabilities are collected on initialization") |
| 51 | { |
| 52 | REQUIRE(ts.keys().empty()); |
| 53 | |
| 54 | std::vector<std::unique_ptr<ICPUInfo::IProvider>> providers; |
| 55 | providers.emplace_back(std::make_unique<ProviderStub>()); |
| 56 | |
| 57 | ts.initialize(providers); |
| 58 | auto keys = ts.keys(); |
| 59 | |
| 60 | REQUIRE(keys.size() == 1); |
| 61 | REQUIRE(keys.front() == "info_key"); |
| 62 | REQUIRE(ts.info("info_key") == "info"); |
| 63 | |
| 64 | REQUIRE(ts.hasCapability("capability")); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | } // namespace Tests::CPUInfo |