tests: add tests for internal utilities
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
		@@ -1,8 +1,8 @@
 | 
			
		||||
#include "memory.hh"
 | 
			
		||||
#include "header.hh"
 | 
			
		||||
#include "util/bits.hh"
 | 
			
		||||
#include "util/crypto.hh"
 | 
			
		||||
#include "util/log.hh"
 | 
			
		||||
#include "util/utils.hh"
 | 
			
		||||
#include <bitset>
 | 
			
		||||
#include <stdexcept>
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -14,19 +14,19 @@ get_bit(Int num, size_t n) {
 | 
			
		||||
template<std::integral Int>
 | 
			
		||||
inline void
 | 
			
		||||
set_bit(Int& num, size_t n) {
 | 
			
		||||
    num |= (1 << n);
 | 
			
		||||
    num |= (static_cast<Int>(1) << n);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
template<std::integral Int>
 | 
			
		||||
inline void
 | 
			
		||||
rst_bit(Int& num, size_t n) {
 | 
			
		||||
    num &= ~(1 << n);
 | 
			
		||||
    num &= ~(static_cast<Int>(1) << n);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
template<std::integral Int>
 | 
			
		||||
inline void
 | 
			
		||||
chg_bit(Int& num, size_t n, bool x) {
 | 
			
		||||
    num = (num & ~(1 << n)) | (x << n);
 | 
			
		||||
    num = (num & ~(static_cast<Int>(1) << n)) | (static_cast<Int>(x) << n);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// read range of bits from start to end inclusive
 | 
			
		||||
@@ -36,5 +36,5 @@ bit_range(Int num, size_t start, size_t end) {
 | 
			
		||||
    // NOTE: we do not require -1 if it is a signed integral
 | 
			
		||||
    Int left =
 | 
			
		||||
      std::numeric_limits<Int>::digits - (std::is_unsigned<Int>::value) - end;
 | 
			
		||||
    return num << left >> (left + start);
 | 
			
		||||
    return static_cast<Int>(num << left) >> (left + start);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,8 @@
 | 
			
		||||
 | 
			
		||||
#include <array>
 | 
			
		||||
#include <bit>
 | 
			
		||||
#include <fmt/core.h>
 | 
			
		||||
#include <iomanip>
 | 
			
		||||
#include <sstream>
 | 
			
		||||
#include <string>
 | 
			
		||||
 | 
			
		||||
// Why I wrote this myself? I do not know
 | 
			
		||||
@@ -14,8 +15,8 @@ using std::rotr;
 | 
			
		||||
template<typename std::size_t N>
 | 
			
		||||
std::string
 | 
			
		||||
sha256(std::array<uint8_t, N>& data) {
 | 
			
		||||
    std::stringstream ss;
 | 
			
		||||
    // Assuming 1 byte = 8 bits
 | 
			
		||||
    std::string string;
 | 
			
		||||
    size_t k = 512 - (N * 8 + 65) % 512;
 | 
			
		||||
    size_t L = N + (65 + k) / 8;
 | 
			
		||||
    size_t i = 0, j = 0;
 | 
			
		||||
@@ -108,12 +109,12 @@ sha256(std::array<uint8_t, N>& data) {
 | 
			
		||||
            h[j] += h0[j];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ss << std::hex << std::setfill('0');
 | 
			
		||||
 | 
			
		||||
    for (j = 0; j < 8; j++)
 | 
			
		||||
        for (i = 0; i < 4; i++)
 | 
			
		||||
            fmt::format_to(std::back_inserter(string),
 | 
			
		||||
                           "{:02x}",
 | 
			
		||||
                           ((h[j] >> (24 - i * 8)) & 0xFF));
 | 
			
		||||
            ss << std::setw(2) << ((h[j] >> (24 - i * 8)) & 0xFF);
 | 
			
		||||
 | 
			
		||||
    return string;
 | 
			
		||||
    return ss.str();
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
@@ -6,12 +6,12 @@
 | 
			
		||||
 | 
			
		||||
namespace logging {
 | 
			
		||||
namespace ansi {
 | 
			
		||||
static constexpr std::string_view RED     = "\033[31m";
 | 
			
		||||
static constexpr std::string_view YELLOW  = "\033[33m";
 | 
			
		||||
static constexpr std::string_view MAGENTA = "\033[35m";
 | 
			
		||||
static constexpr std::string_view WHITE   = "\033[37m";
 | 
			
		||||
static constexpr std::string_view BOLD    = "\033[1m";
 | 
			
		||||
static constexpr std::string_view RESET   = "\033[0m";
 | 
			
		||||
static constexpr auto RED     = "\033[31m";
 | 
			
		||||
static constexpr auto YELLOW  = "\033[33m";
 | 
			
		||||
static constexpr auto MAGENTA = "\033[35m";
 | 
			
		||||
static constexpr auto WHITE   = "\033[37m";
 | 
			
		||||
static constexpr auto BOLD    = "\033[1m";
 | 
			
		||||
static constexpr auto RESET   = "\033[0m";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
using fmt::print;
 | 
			
		||||
@@ -20,8 +20,9 @@ class Logger {
 | 
			
		||||
    using LogLevel = matar::LogLevel;
 | 
			
		||||
 | 
			
		||||
  public:
 | 
			
		||||
    Logger(LogLevel level = LogLevel::Debug)
 | 
			
		||||
      : level(0) {
 | 
			
		||||
    Logger(LogLevel level = LogLevel::Debug, FILE* stream = stderr)
 | 
			
		||||
      : level(0)
 | 
			
		||||
      , stream(stream) {
 | 
			
		||||
        set_level(level);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -69,14 +70,14 @@ class Logger {
 | 
			
		||||
    void set_level(LogLevel level) {
 | 
			
		||||
        this->level = (static_cast<uint8_t>(level) << 1) - 1;
 | 
			
		||||
    }
 | 
			
		||||
    void set_stream(std::ostream& stream) { this->stream = stream; }
 | 
			
		||||
    void set_stream(FILE* stream) { this->stream = stream; }
 | 
			
		||||
 | 
			
		||||
  private:
 | 
			
		||||
    uint8_t level;
 | 
			
		||||
    std::reference_wrapper<std::ostream> stream = std::clog;
 | 
			
		||||
    FILE* stream;
 | 
			
		||||
};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
extern logging::Logger glogger;
 | 
			
		||||
 | 
			
		||||
#define debug(x) logger.debug("{} = {}", #x, x);
 | 
			
		||||
#define debug(x) glogger.debug("{} = {}", #x, x);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user