Skip to content

Commit

Permalink
Log and don’t shoot nulls past the string.
Browse files Browse the repository at this point in the history
  • Loading branch information
eggrobin committed Mar 30, 2024
1 parent 9be3a51 commit 345adf2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion base/cpuid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ std::string CPUVendorIdentificationString() {

std::string ProcessorBrandString() {
std::string result(48, '\0');
for (int n = 0; n < 4; ++n) {
for (int n = 0; n < 3; ++n) {
auto const piece = CPUID(0x80000002 + n, 0);
std::memcpy(&result[n * 16], &piece.eax, 4);
std::memcpy(&result[n * 16 + 4], &piece.ebx, 4);
Expand Down
11 changes: 10 additions & 1 deletion base/cpuid_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "base/cpuid.hpp"

#include "glog/logging.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

Expand All @@ -12,21 +13,28 @@ using ::testing::Not;
using ::testing::Test;
using namespace principia::base::_cpuid;

class CPUIDTest : public Test {};
class CPUIDTest : public Test {
protected:
CPUIDTest() {
google::LogToStderr();
}
};

TEST_F(CPUIDTest, Vendor) {
// This mostly checks that we are getting something from CPUID, since it is
// hard to expect things from the feature flags. This could be expanded to an
// AnyOf as needed if the tests are run on non-Intel processors.
EXPECT_THAT(CPUVendorIdentificationString(),
AnyOf("AuthenticAMD", "GenuineIntel"));
LOG(INFO) << CPUVendorIdentificationString();
}

TEST_F(CPUIDTest, Brand) {
EXPECT_THAT(ProcessorBrandString(),
AnyOf(HasSubstr("Intel(R) Xeon(R)"),
HasSubstr("AMD Ryzen"),
HasSubstr("VirtualApple")));
LOG(INFO) << ProcessorBrandString();
}

TEST_F(CPUIDTest, CPUFeatureFlags) {
Expand All @@ -42,6 +50,7 @@ TEST_F(CPUIDTest, CPUFeatureFlags) {
EXPECT_THAT(
CPUFeatures(),
AllOf(HasSubstr("FPU"), HasSubstr("SSE2"), Not(HasSubstr("PSN"))));
LOG(INFO) << CPUFeatures();
}

} // namespace base
Expand Down

0 comments on commit 345adf2

Please sign in to comment.