Add unit tests
This commit is contained in:
parent
8d2edc40ca
commit
98c036f9e3
14 changed files with 263 additions and 126 deletions
154
lib/btclock/data_handler.cpp
Normal file
154
lib/btclock/data_handler.cpp
Normal file
|
@ -0,0 +1,154 @@
|
|||
#include "data_handler.hpp"
|
||||
|
||||
std::array<std::string, NUM_SCREENS> parsePriceData(uint price, char currencySymbol)
|
||||
{
|
||||
std::array<std::string, NUM_SCREENS> ret;
|
||||
std::string priceString = currencySymbol + std::to_string(price);
|
||||
uint firstIndex = 0;
|
||||
if (priceString.length() < (NUM_SCREENS))
|
||||
{
|
||||
priceString.insert(priceString.begin(), NUM_SCREENS - priceString.length(), ' ');
|
||||
if (currencySymbol == '[')
|
||||
{
|
||||
ret[0] = "BTC/EUR";
|
||||
}
|
||||
else
|
||||
{
|
||||
ret[0] = "BTC/USD";
|
||||
}
|
||||
firstIndex = 1;
|
||||
}
|
||||
|
||||
for (uint i = firstIndex; i < NUM_SCREENS; i++)
|
||||
{
|
||||
ret[i] = priceString[i];
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::array<std::string, NUM_SCREENS> parseSatsPerCurrency(uint price, char currencySymbol)
|
||||
{
|
||||
std::array<std::string, NUM_SCREENS> ret;
|
||||
std::string priceString = std::to_string(int(round(1 / float(price) * 10e7)));
|
||||
uint firstIndex = 0;
|
||||
|
||||
if (priceString.length() < (NUM_SCREENS))
|
||||
{
|
||||
priceString.insert(priceString.begin(), NUM_SCREENS - priceString.length(), ' ');
|
||||
if (currencySymbol == '[')
|
||||
{
|
||||
ret[0] = "SATS/EUR";
|
||||
}
|
||||
else
|
||||
{
|
||||
ret[0] = "MSCW/TIME";
|
||||
}
|
||||
firstIndex = 1;
|
||||
|
||||
for (uint i = firstIndex; i < NUM_SCREENS; i++)
|
||||
{
|
||||
ret[i] = priceString[i];
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::array<std::string, NUM_SCREENS> parseBlockHeight(uint blockHeight)
|
||||
{
|
||||
std::array<std::string, NUM_SCREENS> ret;
|
||||
std::string blockNrString = std::to_string(blockHeight);
|
||||
uint firstIndex = 0;
|
||||
|
||||
if (blockNrString.length() < NUM_SCREENS)
|
||||
{
|
||||
blockNrString.insert(blockNrString.begin(), NUM_SCREENS - blockNrString.length(), ' ');
|
||||
ret[0] = "BLOCK/HEIGHT";
|
||||
firstIndex = 1;
|
||||
}
|
||||
|
||||
for (uint i = firstIndex; i < NUM_SCREENS; i++)
|
||||
{
|
||||
ret[i] = blockNrString[i];
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::array<std::string, NUM_SCREENS> parseHalvingCountdown(uint blockHeight)
|
||||
{
|
||||
std::array<std::string, NUM_SCREENS> ret;
|
||||
|
||||
const uint nextHalvingBlock = 210000 - (blockHeight % 210000);
|
||||
const uint minutesToHalving = nextHalvingBlock * 10;
|
||||
|
||||
const int years = floor(minutesToHalving / 525600);
|
||||
const int days = floor((minutesToHalving - (years * 525600)) / (24 * 60));
|
||||
const int hours = floor((minutesToHalving - (years * 525600) - (days * (24 * 60))) / 60);
|
||||
const int mins = floor(minutesToHalving - (years * 525600) - (days * (24 * 60)) - (hours * 60));
|
||||
ret[0] = "BIT/COIN";
|
||||
ret[1] = "HALV/ING";
|
||||
ret[(NUM_SCREENS - 5)] = std::to_string(years) + "/YRS";
|
||||
ret[(NUM_SCREENS - 4)] = std::to_string(days) + "/DAYS";
|
||||
ret[(NUM_SCREENS - 3)] = std::to_string(hours) + "/HRS";
|
||||
ret[(NUM_SCREENS - 2)] = std::to_string(mins) + "/MINS";
|
||||
ret[(NUM_SCREENS - 1)] = "TO/GO";
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::array<std::string, NUM_SCREENS> parseMarketCap(uint blockHeight, uint price, char currencySymbol, bool bigChars)
|
||||
{
|
||||
std::array<std::string, NUM_SCREENS> ret;
|
||||
uint firstIndex = 0;
|
||||
double supply = getSupplyAtBlock(blockHeight);
|
||||
int64_t marketCap = static_cast<std::int64_t>(supply * double(price));
|
||||
if (currencySymbol == '[')
|
||||
{
|
||||
ret[0] = "EUR/MCAP";
|
||||
}
|
||||
else
|
||||
{
|
||||
ret[0] = "USD/MCAP";
|
||||
}
|
||||
|
||||
if (bigChars)
|
||||
{
|
||||
firstIndex = 1;
|
||||
|
||||
std::string priceString = currencySymbol + formatNumberWithSuffix(marketCap);
|
||||
priceString.insert(priceString.begin(), NUM_SCREENS - priceString.length(), ' ');
|
||||
|
||||
for (uint i = firstIndex; i < NUM_SCREENS; i++)
|
||||
{
|
||||
ret[i] = priceString[i];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string stringValue = std::to_string(marketCap);
|
||||
size_t mcLength = stringValue.length();
|
||||
size_t leadingSpaces = (3 - mcLength % 3) % 3;
|
||||
stringValue = std::string(leadingSpaces, ' ') + stringValue;
|
||||
|
||||
uint groups = (mcLength + leadingSpaces) / 3;
|
||||
|
||||
if (groups < NUM_SCREENS)
|
||||
{
|
||||
firstIndex = 1;
|
||||
}
|
||||
|
||||
for (int i = firstIndex; i < NUM_SCREENS - groups - 1; i++)
|
||||
{
|
||||
ret[i] = "";
|
||||
}
|
||||
|
||||
ret[NUM_SCREENS - groups - 1] = " $ ";
|
||||
for (uint i = 0; i < groups; i++)
|
||||
{
|
||||
ret[(NUM_SCREENS - groups + i)] = stringValue.substr(i * 3, 3).c_str();
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
10
lib/btclock/data_handler.hpp
Normal file
10
lib/btclock/data_handler.hpp
Normal file
|
@ -0,0 +1,10 @@
|
|||
#include <array>
|
||||
#include <string>
|
||||
#include <cmath>
|
||||
#include "utils.hpp"
|
||||
|
||||
std::array<std::string, NUM_SCREENS> parsePriceData(uint price, char currencySymbol);
|
||||
std::array<std::string, NUM_SCREENS> parseSatsPerCurrency(uint price, char currencySymbol);
|
||||
std::array<std::string, NUM_SCREENS> parseBlockHeight(uint blockHeight);
|
||||
std::array<std::string, NUM_SCREENS> parseHalvingCountdown(uint blockHeight);
|
||||
std::array<std::string, NUM_SCREENS> parseMarketCap(uint blockHeight, uint price, char currencySymbol, bool bigChars);
|
48
lib/btclock/utils.cpp
Normal file
48
lib/btclock/utils.cpp
Normal file
|
@ -0,0 +1,48 @@
|
|||
#include "utils.hpp"
|
||||
|
||||
int modulo(int x, int N)
|
||||
{
|
||||
return (x % N + N) % N;
|
||||
}
|
||||
|
||||
double getSupplyAtBlock(uint blockNr) {
|
||||
if (blockNr >= 33 * 210000) {
|
||||
return 20999999.9769;
|
||||
}
|
||||
|
||||
const int initialBlockReward = 50; // Initial block reward
|
||||
const int halvingInterval = 210000; // Number of blocks before halving
|
||||
|
||||
int halvingCount = blockNr / halvingInterval;
|
||||
double totalBitcoinInCirculation = 0;
|
||||
|
||||
for (int i = 0; i < halvingCount; ++i) {
|
||||
totalBitcoinInCirculation += halvingInterval * initialBlockReward * std::pow(0.5, i);
|
||||
}
|
||||
|
||||
totalBitcoinInCirculation += (blockNr % halvingInterval) * initialBlockReward * std::pow(0.5, halvingCount);
|
||||
|
||||
return totalBitcoinInCirculation;
|
||||
}
|
||||
|
||||
std::string formatNumberWithSuffix(int64_t num) {
|
||||
const long long quadrillion = 1000000000000000LL;
|
||||
const long long trillion = 1000000000000LL;
|
||||
const long long billion = 1000000000;
|
||||
const long long million = 1000000;
|
||||
const long long thousand = 1000;
|
||||
|
||||
if (num >= quadrillion) {
|
||||
return std::to_string(num / quadrillion) + "Q";
|
||||
} else if (num >= trillion) {
|
||||
return std::to_string(num / trillion) + "T";
|
||||
} else if (num >= billion) {
|
||||
return std::to_string(num / billion) + "B";
|
||||
} else if (num >= million) {
|
||||
return std::to_string(num / million) + "M";
|
||||
} else if (num >= thousand) {
|
||||
return std::to_string(num / thousand) + "K";
|
||||
} else {
|
||||
return std::to_string(num);
|
||||
}
|
||||
}
|
10
lib/btclock/utils.hpp
Normal file
10
lib/btclock/utils.hpp
Normal file
|
@ -0,0 +1,10 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <cmath>
|
||||
|
||||
int modulo(int x,int N);
|
||||
|
||||
double getSupplyAtBlock(uint blockNr);
|
||||
|
||||
std::string formatNumberWithSuffix(int64_t num);
|
Loading…
Add table
Add a link
Reference in a new issue