Make unit tests on native possible
This commit is contained in:
parent
98c036f9e3
commit
f05b848030
6 changed files with 69 additions and 42 deletions
|
@ -1,10 +1,15 @@
|
|||
#include "data_handler.hpp"
|
||||
|
||||
std::array<std::string, NUM_SCREENS> parsePriceData(uint price, char currencySymbol)
|
||||
std::array<std::string, NUM_SCREENS> parsePriceData(std::uint32_t price, char currencySymbol)
|
||||
{
|
||||
std::array<std::string, NUM_SCREENS> ret;
|
||||
std::string priceString = currencySymbol + std::to_string(price);
|
||||
uint firstIndex = 0;
|
||||
std::string priceString;
|
||||
if (std::to_string(price).length() >= NUM_SCREENS) {
|
||||
priceString = formatNumberWithSuffix(price);
|
||||
} else {
|
||||
priceString = currencySymbol + std::to_string(price);
|
||||
}
|
||||
std::uint32_t firstIndex = 0;
|
||||
if (priceString.length() < (NUM_SCREENS))
|
||||
{
|
||||
priceString.insert(priceString.begin(), NUM_SCREENS - priceString.length(), ' ');
|
||||
|
@ -19,7 +24,7 @@ std::array<std::string, NUM_SCREENS> parsePriceData(uint price, char currencySym
|
|||
firstIndex = 1;
|
||||
}
|
||||
|
||||
for (uint i = firstIndex; i < NUM_SCREENS; i++)
|
||||
for (std::uint32_t i = firstIndex; i < NUM_SCREENS; i++)
|
||||
{
|
||||
ret[i] = priceString[i];
|
||||
}
|
||||
|
@ -27,11 +32,11 @@ std::array<std::string, NUM_SCREENS> parsePriceData(uint price, char currencySym
|
|||
return ret;
|
||||
}
|
||||
|
||||
std::array<std::string, NUM_SCREENS> parseSatsPerCurrency(uint price, char currencySymbol)
|
||||
std::array<std::string, NUM_SCREENS> parseSatsPerCurrency(std::uint32_t 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;
|
||||
std::uint32_t firstIndex = 0;
|
||||
|
||||
if (priceString.length() < (NUM_SCREENS))
|
||||
{
|
||||
|
@ -46,7 +51,7 @@ std::array<std::string, NUM_SCREENS> parseSatsPerCurrency(uint price, char curre
|
|||
}
|
||||
firstIndex = 1;
|
||||
|
||||
for (uint i = firstIndex; i < NUM_SCREENS; i++)
|
||||
for (std::uint32_t i = firstIndex; i < NUM_SCREENS; i++)
|
||||
{
|
||||
ret[i] = priceString[i];
|
||||
}
|
||||
|
@ -54,11 +59,11 @@ std::array<std::string, NUM_SCREENS> parseSatsPerCurrency(uint price, char curre
|
|||
return ret;
|
||||
}
|
||||
|
||||
std::array<std::string, NUM_SCREENS> parseBlockHeight(uint blockHeight)
|
||||
std::array<std::string, NUM_SCREENS> parseBlockHeight(std::uint32_t blockHeight)
|
||||
{
|
||||
std::array<std::string, NUM_SCREENS> ret;
|
||||
std::string blockNrString = std::to_string(blockHeight);
|
||||
uint firstIndex = 0;
|
||||
std::uint32_t firstIndex = 0;
|
||||
|
||||
if (blockNrString.length() < NUM_SCREENS)
|
||||
{
|
||||
|
@ -67,7 +72,7 @@ std::array<std::string, NUM_SCREENS> parseBlockHeight(uint blockHeight)
|
|||
firstIndex = 1;
|
||||
}
|
||||
|
||||
for (uint i = firstIndex; i < NUM_SCREENS; i++)
|
||||
for (std::uint32_t i = firstIndex; i < NUM_SCREENS; i++)
|
||||
{
|
||||
ret[i] = blockNrString[i];
|
||||
}
|
||||
|
@ -75,12 +80,12 @@ std::array<std::string, NUM_SCREENS> parseBlockHeight(uint blockHeight)
|
|||
return ret;
|
||||
}
|
||||
|
||||
std::array<std::string, NUM_SCREENS> parseHalvingCountdown(uint blockHeight)
|
||||
std::array<std::string, NUM_SCREENS> parseHalvingCountdown(std::uint32_t blockHeight)
|
||||
{
|
||||
std::array<std::string, NUM_SCREENS> ret;
|
||||
|
||||
const uint nextHalvingBlock = 210000 - (blockHeight % 210000);
|
||||
const uint minutesToHalving = nextHalvingBlock * 10;
|
||||
const std::uint32_t nextHalvingBlock = 210000 - (blockHeight % 210000);
|
||||
const std::uint32_t minutesToHalving = nextHalvingBlock * 10;
|
||||
|
||||
const int years = floor(minutesToHalving / 525600);
|
||||
const int days = floor((minutesToHalving - (years * 525600)) / (24 * 60));
|
||||
|
@ -97,10 +102,10 @@ std::array<std::string, NUM_SCREENS> parseHalvingCountdown(uint blockHeight)
|
|||
return ret;
|
||||
}
|
||||
|
||||
std::array<std::string, NUM_SCREENS> parseMarketCap(uint blockHeight, uint price, char currencySymbol, bool bigChars)
|
||||
std::array<std::string, NUM_SCREENS> parseMarketCap(std::uint32_t blockHeight, std::uint32_t price, char currencySymbol, bool bigChars)
|
||||
{
|
||||
std::array<std::string, NUM_SCREENS> ret;
|
||||
uint firstIndex = 0;
|
||||
std::uint32_t firstIndex = 0;
|
||||
double supply = getSupplyAtBlock(blockHeight);
|
||||
int64_t marketCap = static_cast<std::int64_t>(supply * double(price));
|
||||
if (currencySymbol == '[')
|
||||
|
@ -119,7 +124,7 @@ std::array<std::string, NUM_SCREENS> parseMarketCap(uint blockHeight, uint price
|
|||
std::string priceString = currencySymbol + formatNumberWithSuffix(marketCap);
|
||||
priceString.insert(priceString.begin(), NUM_SCREENS - priceString.length(), ' ');
|
||||
|
||||
for (uint i = firstIndex; i < NUM_SCREENS; i++)
|
||||
for (std::uint32_t i = firstIndex; i < NUM_SCREENS; i++)
|
||||
{
|
||||
ret[i] = priceString[i];
|
||||
}
|
||||
|
@ -131,7 +136,7 @@ std::array<std::string, NUM_SCREENS> parseMarketCap(uint blockHeight, uint price
|
|||
size_t leadingSpaces = (3 - mcLength % 3) % 3;
|
||||
stringValue = std::string(leadingSpaces, ' ') + stringValue;
|
||||
|
||||
uint groups = (mcLength + leadingSpaces) / 3;
|
||||
std::uint32_t groups = (mcLength + leadingSpaces) / 3;
|
||||
|
||||
if (groups < NUM_SCREENS)
|
||||
{
|
||||
|
@ -144,7 +149,7 @@ std::array<std::string, NUM_SCREENS> parseMarketCap(uint blockHeight, uint price
|
|||
}
|
||||
|
||||
ret[NUM_SCREENS - groups - 1] = " $ ";
|
||||
for (uint i = 0; i < groups; i++)
|
||||
for (std::uint32_t i = 0; i < groups; i++)
|
||||
{
|
||||
ret[(NUM_SCREENS - groups + i)] = stringValue.substr(i * 3, 3).c_str();
|
||||
}
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
#include <array>
|
||||
#include <string>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
|
||||
#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);
|
||||
std::array<std::string, NUM_SCREENS> parsePriceData(std::uint32_t price, char currencySymbol);
|
||||
std::array<std::string, NUM_SCREENS> parseSatsPerCurrency(std::uint32_t price, char currencySymbol);
|
||||
std::array<std::string, NUM_SCREENS> parseBlockHeight(std::uint32_t blockHeight);
|
||||
std::array<std::string, NUM_SCREENS> parseHalvingCountdown(std::uint32_t blockHeight);
|
||||
std::array<std::string, NUM_SCREENS> parseMarketCap(std::uint32_t blockHeight, std::uint32_t price, char currencySymbol, bool bigChars);
|
|
@ -5,7 +5,7 @@ int modulo(int x, int N)
|
|||
return (x % N + N) % N;
|
||||
}
|
||||
|
||||
double getSupplyAtBlock(uint blockNr) {
|
||||
double getSupplyAtBlock(std::uint32_t blockNr) {
|
||||
if (blockNr >= 33 * 210000) {
|
||||
return 20999999.9769;
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ double getSupplyAtBlock(uint blockNr) {
|
|||
return totalBitcoinInCirculation;
|
||||
}
|
||||
|
||||
std::string formatNumberWithSuffix(int64_t num) {
|
||||
std::string formatNumberWithSuffix(std::uint64_t num) {
|
||||
const long long quadrillion = 1000000000000000LL;
|
||||
const long long trillion = 1000000000000LL;
|
||||
const long long billion = 1000000000;
|
||||
|
|
|
@ -2,9 +2,10 @@
|
|||
|
||||
#include <string>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
|
||||
int modulo(int x,int N);
|
||||
|
||||
double getSupplyAtBlock(uint blockNr);
|
||||
double getSupplyAtBlock(std::uint32_t blockNr);
|
||||
|
||||
std::string formatNumberWithSuffix(int64_t num);
|
||||
std::string formatNumberWithSuffix(std::uint64_t num);
|
|
@ -11,13 +11,14 @@
|
|||
data_dir = data/build_gz
|
||||
|
||||
[env]
|
||||
|
||||
|
||||
[btclock_base]
|
||||
platform = https://github.com/platformio/platform-espressif32.git
|
||||
framework = arduino, espidf
|
||||
monitor_speed = 115200
|
||||
monitor_filters = esp32_exception_decoder, colorize
|
||||
board_build.filesystem = littlefs
|
||||
|
||||
[btclock_base]
|
||||
extra_scripts = post:scripts/extra_script.py
|
||||
build_flags =
|
||||
!python scripts/git_rev.py
|
||||
|
@ -84,4 +85,14 @@ build_flags =
|
|||
-D MCP2_A1_PIN=10
|
||||
-D MCP2_A2_PIN=14
|
||||
build_unflags =
|
||||
${btclock_base.build_unflags}
|
||||
${btclock_base.build_unflags}
|
||||
|
||||
[env:native_test_only]
|
||||
platform = native
|
||||
test_framework = unity
|
||||
build_flags =
|
||||
${btclock_base.build_flags}
|
||||
-D MCP_INT_PIN=8
|
||||
-D NEOPIXEL_PIN=34
|
||||
-D NEOPIXEL_COUNT=4
|
||||
-D NUM_SCREENS=7
|
|
@ -9,7 +9,7 @@ void tearDown(void) {
|
|||
// clean stuff up here
|
||||
}
|
||||
|
||||
void test_sats_per_dollar(void) {
|
||||
void test_CorrectSatsPerDollarConversion(void) {
|
||||
std::array<std::string, NUM_SCREENS> output = parseSatsPerCurrency(37253, '$');
|
||||
TEST_ASSERT_EQUAL_STRING("MSCW/TIME", output[0].c_str());
|
||||
TEST_ASSERT_EQUAL_STRING("2", output[NUM_SCREENS-4].c_str());
|
||||
|
@ -19,42 +19,50 @@ void test_sats_per_dollar(void) {
|
|||
}
|
||||
|
||||
|
||||
void test_block_height_6screens(void) {
|
||||
void test_SixCharacterBlockHeight(void) {
|
||||
std::array<std::string, NUM_SCREENS> output = parseBlockHeight(999999);
|
||||
TEST_ASSERT_EQUAL_STRING("BLOCK/HEIGHT", output[0].c_str());
|
||||
TEST_ASSERT_EQUAL_STRING("9", output[1].c_str());
|
||||
}
|
||||
|
||||
void test_block_height_7screens(void) {
|
||||
void test_SevenCharacterBlockHeight(void) {
|
||||
std::array<std::string, NUM_SCREENS> output = parseBlockHeight(1000000);
|
||||
TEST_ASSERT_EQUAL_STRING("1", output[0].c_str());
|
||||
TEST_ASSERT_EQUAL_STRING("0", output[1].c_str());
|
||||
}
|
||||
|
||||
void test_ticker_6screens(void) {
|
||||
void test_PriceOf100kusd(void) {
|
||||
std::array<std::string, NUM_SCREENS> output = parsePriceData(100000, '$');
|
||||
TEST_ASSERT_EQUAL_STRING("$", output[0].c_str());
|
||||
TEST_ASSERT_EQUAL_STRING("1", output[1].c_str());
|
||||
}
|
||||
|
||||
void test_ticker_7screens(void) {
|
||||
void test_PriceOf1MillionUsd(void) {
|
||||
std::array<std::string, NUM_SCREENS> output = parsePriceData(1000000, '$');
|
||||
TEST_ASSERT_EQUAL_STRING("1", output[0].c_str());
|
||||
TEST_ASSERT_EQUAL_STRING("0", output[1].c_str());
|
||||
TEST_ASSERT_EQUAL_STRING("BTC/USD", output[0].c_str());
|
||||
for (int i = 1; i <= NUM_SCREENS-3; i++) {
|
||||
TEST_ASSERT_EQUAL_STRING(" ", output[i].c_str());
|
||||
}
|
||||
TEST_ASSERT_EQUAL_STRING("1", output[NUM_SCREENS-2].c_str());
|
||||
TEST_ASSERT_EQUAL_STRING("M", output[NUM_SCREENS-1].c_str());
|
||||
}
|
||||
|
||||
// not needed when using generate_test_runner.rb
|
||||
int runUnityTests(void) {
|
||||
UNITY_BEGIN();
|
||||
RUN_TEST(test_sats_per_dollar);
|
||||
RUN_TEST(test_block_height_6screens);
|
||||
RUN_TEST(test_block_height_7screens);
|
||||
RUN_TEST(test_ticker_6screens);
|
||||
RUN_TEST(test_ticker_7screens);
|
||||
RUN_TEST(test_CorrectSatsPerDollarConversion);
|
||||
RUN_TEST(test_SixCharacterBlockHeight);
|
||||
RUN_TEST(test_SevenCharacterBlockHeight);
|
||||
RUN_TEST(test_PriceOf100kusd);
|
||||
RUN_TEST(test_PriceOf1MillionUsd);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
return runUnityTests();
|
||||
}
|
||||
|
||||
extern "C" void app_main() {
|
||||
runUnityTests();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue