libcpuid/Sanity/basic-test/test_libcpuid.c
František Hrdina a50bf8e1b0 Add basic test
2024-07-29 14:56:31 +02:00

64 lines
1.7 KiB
C

#include <stdio.h>
#include <string.h>
#include <libcpuid.h>
int main() {
struct cpu_raw_data_t raw;
struct cpu_id_t data;
double cpu_clock;
// Get raw CPU data
if (cpuid_get_raw_data(&raw) < 0) {
fprintf(stderr, "Error getting raw CPU data: %s\n", cpuid_error());
return 1;
}
// Decode data
if (cpu_identify(&raw, &data) < 0) {
fprintf(stderr, "Error identifying CPU: %s\n", cpuid_error());
return 1;
}
// Get current clock frequency
cpu_clock = cpu_clock_by_os();
if (cpu_clock < 0) {
fprintf(stderr, "Error retrieving clock frequency: %s\n", cpuid_error());
return 1;
}
// Check if vendor and model are non-empty strings
if (data.vendor_str == NULL || strlen(data.vendor_str) == 0) {
fprintf(stderr, "Error: Vendor string is empty or NULL.\n");
return 1;
}
if (data.brand_str == NULL || strlen(data.brand_str) == 0) {
fprintf(stderr, "Error: Model string is empty or NULL.\n");
return 1;
}
if (data.num_cores <= 0) {
fprintf(stderr, "Error: Number of cores is not greater than 0.\n");
return 1;
}
if (data.num_logical_cpus <= 0) {
fprintf(stderr, "Error: Number of logical CPUs is not greater than 0.\n");
return 1;
}
// Print basic CPU information
printf("Vendor: %s\n", data.vendor_str);
printf("Model: %s\n", data.brand_str);
printf("Family: %d\n", data.family);
printf("Model: %d\n", data.model);
printf("Stepping: %d\n", data.stepping);
printf("Current clock frequency: %.2f MHz\n", cpu_clock);
printf("Cores: %d\n", data.num_cores);
printf("Logical CPUs: %d\n", data.num_logical_cpus);
return 0;
}