Skip to content

Commit

Permalink
update testing tools
Browse files Browse the repository at this point in the history
  • Loading branch information
i80287 committed Jul 18, 2024
1 parent 7d51ee8 commit e4962f3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
22 changes: 9 additions & 13 deletions number_theory/config_macros.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
#ifndef CONFIG_MACROS_HPP
#define CONFIG_MACROS_HPP 1

#ifdef __has_include
#if __has_include(<version>)
#include <version>
#elif __has_include(<ciso646>)
#include <ciso646>
#elif __has_include(<iso646.h>)
#include <iso646.h>
#endif
#endif

/* Test for gcc >= maj.min, as per __GNUC_PREREQ in glibc */
#if defined(__GNUC__) && defined(__GNUC_MINOR__)
#define CONFIG_GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
Expand Down Expand Up @@ -70,12 +60,18 @@
#define CONFIG_HAS_BUILTIN(name) 0
#endif

#if CONFIG_HAS_INCLUDE(<version>)
#include <version>
#elif CONFIG_HAS_INCLUDE(<ciso646>)
#include <ciso646>
#elif CONFIG_HAS_INCLUDE(<iso646.h>)
#include <iso646.h>
#endif

/**
* Restrict qualifier for the C++ (C has `restrict` keyword since C99)
*/
#if defined(__GNUC__)
#define RESTRICT_QUALIFIER __restrict__
#elif defined(__clang__)
#if defined(__GNUC__) || defined(__clang__)
#define RESTRICT_QUALIFIER __restrict__
#elif defined(_MSC_VER)
#define RESTRICT_QUALIFIER __restrict
Expand Down
6 changes: 2 additions & 4 deletions number_theory/measure_is_prime_bpsw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ using namespace test_tools;
static std::vector<uint64_t> read_primes() {
std::vector<uint64_t> nums;
nums.reserve(1065000zu);
Wrapper fin("u64-primes.txt", "r");
while (true) {
uint64_t n = 0;
switch (fscanf(fin.file, "%" PRIu64, &n)) {
for (Wrapper fin("u64-primes.txt", "r");;) {
switch (uint64_t n = 0; fscanf(fin.file, "%" PRIu64, &n)) {
[[likely]] case 1:
nums.push_back(n);
break;
Expand Down
16 changes: 8 additions & 8 deletions number_theory/test_tools.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#pragma once

#ifdef NDEBUG
#warning "Can't test properly with NDEBUG macro defined (macro won't be undefined manually)"
#warning("Can't test properly with NDEBUG macro defined (macro won't be undefined manually)")
#endif

#include <array>
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <stdexcept>

#include "config_macros.hpp"
Expand All @@ -20,12 +22,12 @@ template <class T>
[[noreturn]] ATTRIBUTE_COLD inline void throw_impl(const char* message_format, T arg,
const char* file_name, uint32_t line,
const char* function_name) {
std::array<char, 1024> buffer;
std::array<char, 1024> buffer{};
int bytes_written = std::snprintf(buffer.data(), buffer.size(),
"Check failed at %s:%u %s\nError messssage: ", file_name,
line, function_name);
size_t err_msg_offset = uint32_t(bytes_written);
if (bytes_written < 0 || err_msg_offset > buffer.size()) {
if (err_msg_offset > buffer.size()) {
perror("std::snprintf");
err_msg_offset = 0;
}
Expand Down Expand Up @@ -102,21 +104,19 @@ struct Wrapper final {

private:
[[noreturn]] ATTRIBUTE_COLD static void ThrowOnFOpenFail(const char* fname, const char* mode) {
std::array<char, 1024> buffer;
std::array<char, 1024> buffer{};
int bytes_written = std::snprintf(
buffer.data(), buffer.size(),
"Wrapper::Wrapper(const char* fname, const char* mode): std::fopen(%s, %s) failed",
fname, mode);
"Wrapper::Wrapper(const char* fname, const char* mode): std::fopen(\"%s\", \"%s\") failed: %s",
fname, mode, strerror(errno));
if (bytes_written < 0) [[unlikely]] {
constexpr std::string_view msg =
"Wrapper::Wrapper(const char* fname,const char* mode): "
"std::snprintf failed after std::fopen failed";
static_assert(msg.size() <= std::size(buffer),
"Wrapper::Wrapper(const char*,const char*)");
std::char_traits<char>::copy(buffer.data(), msg.data(), msg.size());
perror(buffer.data());
}
perror(buffer.data());
throw std::runtime_error(buffer.data());
}
};
Expand Down

0 comments on commit e4962f3

Please sign in to comment.