mirror of
https://github.com/btclock/btclock_v3.git
synced 2024-11-19 02:30:01 +01:00
Add unit tests
This commit is contained in:
parent
8d2edc40ca
commit
98c036f9e3
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);
|
@ -5,17 +5,6 @@ int modulo(int x, int N)
|
||||
return (x % N + N) % N;
|
||||
}
|
||||
|
||||
String getMyHostname() {
|
||||
uint8_t mac[6];
|
||||
//WiFi.macAddress(mac);
|
||||
esp_efuse_mac_get_default(mac);
|
||||
char hostname[15];
|
||||
String hostnamePrefix = preferences.getString("hostnamePrefix", "btclock");
|
||||
snprintf(hostname, sizeof(hostname), "%s-%02x%02x%02x",
|
||||
hostnamePrefix, mac[3], mac[4], mac[5]);
|
||||
return hostname;
|
||||
}
|
||||
|
||||
double getSupplyAtBlock(uint blockNr) {
|
||||
if (blockNr >= 33 * 210000) {
|
||||
return 20999999.9769;
|
@ -1,11 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <WiFi.h>
|
||||
#include "shared.hpp"
|
||||
#include <string>
|
||||
#include <cmath>
|
||||
|
||||
int modulo(int x,int N);
|
||||
|
||||
double getSupplyAtBlock(uint blockNr);
|
||||
|
||||
String getMyHostname();
|
||||
std::string formatNumberWithSuffix(int64_t num);
|
@ -56,6 +56,7 @@ build_unflags =
|
||||
|
||||
[env:lolin_s3_mini_qr]
|
||||
extends = env:lolin_s3_mini
|
||||
test_framework = unity
|
||||
build_flags =
|
||||
${env:lolin_s3_mini.build_flags}
|
||||
-D USE_QR
|
||||
@ -64,6 +65,7 @@ build_flags =
|
||||
extends = btclock_base
|
||||
board = btclock
|
||||
board_build.partitions = partition_16mb.csv
|
||||
test_framework = unity
|
||||
build_flags =
|
||||
${btclock_base.build_flags}
|
||||
-D MCP_INT_PIN=4
|
||||
|
@ -605,4 +605,15 @@ void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info)
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
String getMyHostname() {
|
||||
uint8_t mac[6];
|
||||
//WiFi.macAddress(mac);
|
||||
esp_efuse_mac_get_default(mac);
|
||||
char hostname[15];
|
||||
String hostnamePrefix = preferences.getString("hostnamePrefix", "btclock");
|
||||
snprintf(hostname, sizeof(hostname), "%s-%02x%02x%02x",
|
||||
hostnamePrefix, mac[3], mac[4], mac[5]);
|
||||
return hostname;
|
||||
}
|
@ -42,6 +42,7 @@ void tryImprovSetup();
|
||||
void setupTimers();
|
||||
void finishSetup();
|
||||
void setupMcp();
|
||||
String getMyHostname();
|
||||
std::vector<std::string> getScreenNameMap();
|
||||
|
||||
std::vector<std::string> getLocalUrl();
|
||||
|
@ -173,6 +173,16 @@ void setEpdContent(std::array<String, NUM_SCREENS> newEpdContent)
|
||||
setEpdContent(newEpdContent, false);
|
||||
}
|
||||
|
||||
void setEpdContent(std::array<std::string, NUM_SCREENS> newEpdContent) {
|
||||
std::array<String, NUM_SCREENS> conv;
|
||||
|
||||
for (size_t i = 0; i < newEpdContent.size(); ++i) {
|
||||
conv[i] = String(newEpdContent[i].c_str());
|
||||
}
|
||||
|
||||
return setEpdContent(conv);
|
||||
}
|
||||
|
||||
void setEpdContent(std::array<String, NUM_SCREENS> newEpdContent, bool forceUpdate)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(epdUpdateMutex);
|
||||
|
@ -44,5 +44,8 @@ void renderQr(const uint dispNum, const String& text, bool partial);
|
||||
|
||||
void setEpdContent(std::array<String, NUM_SCREENS> newEpdContent, bool forceUpdate);
|
||||
void setEpdContent(std::array<String, NUM_SCREENS> newEpdContent);
|
||||
|
||||
void setEpdContent(std::array<std::string, NUM_SCREENS> newEpdContent);
|
||||
|
||||
std::array<String, NUM_SCREENS> getCurrentEpdContent();
|
||||
void waitUntilNoneBusy();
|
@ -8,7 +8,7 @@ TaskHandle_t workerTaskHandle;
|
||||
esp_timer_handle_t screenRotateTimer;
|
||||
esp_timer_handle_t minuteTimer;
|
||||
|
||||
std::array<String, NUM_SCREENS> taskEpdContent = {"", "", "", "", "", "", ""};
|
||||
std::array<std::string, NUM_SCREENS> taskEpdContent = {"", "", "", "", "", "", ""};
|
||||
std::string priceString;
|
||||
|
||||
// typedef enum
|
||||
@ -45,7 +45,6 @@ void workerTask(void *pvParameters)
|
||||
{
|
||||
case TASK_PRICE_UPDATE:
|
||||
{
|
||||
firstIndex = 0;
|
||||
uint price = getPrice();
|
||||
char priceSymbol = '$';
|
||||
if (preferences.getBool("fetchEurPrice", false))
|
||||
@ -54,92 +53,15 @@ void workerTask(void *pvParameters)
|
||||
}
|
||||
if (getCurrentScreen() == SCREEN_BTC_TICKER)
|
||||
{
|
||||
priceString = (priceSymbol + String(price)).c_str();
|
||||
|
||||
if (priceString.length() < (NUM_SCREENS))
|
||||
{
|
||||
priceString.insert(priceString.begin(), NUM_SCREENS - priceString.length(), ' ');
|
||||
if (preferences.getBool("fetchEurPrice", false))
|
||||
{
|
||||
taskEpdContent[0] = "BTC/EUR";
|
||||
}
|
||||
else
|
||||
{
|
||||
taskEpdContent[0] = "BTC/USD";
|
||||
}
|
||||
firstIndex = 1;
|
||||
}
|
||||
taskEpdContent = parsePriceData(price, priceSymbol);
|
||||
}
|
||||
else if (getCurrentScreen() == SCREEN_MSCW_TIME)
|
||||
{
|
||||
priceString = String(int(round(1 / float(price) * 10e7))).c_str();
|
||||
|
||||
if (priceString.length() < (NUM_SCREENS))
|
||||
{
|
||||
priceString.insert(priceString.begin(), NUM_SCREENS - priceString.length(), ' ');
|
||||
if (preferences.getBool("fetchEurPrice", false))
|
||||
{
|
||||
taskEpdContent[0] = "SATS/EUR";
|
||||
} else {
|
||||
taskEpdContent[0] = "MSCW/TIME";
|
||||
}
|
||||
firstIndex = 1;
|
||||
}
|
||||
taskEpdContent = parseSatsPerCurrency(price, priceSymbol);
|
||||
}
|
||||
else
|
||||
{
|
||||
double supply = getSupplyAtBlock(getBlockHeight());
|
||||
int64_t marketCap = static_cast<std::int64_t>(supply * double(price));
|
||||
|
||||
if (preferences.getBool("fetchEurPrice", false))
|
||||
{
|
||||
taskEpdContent[0] = "EUR/MCAP";
|
||||
}
|
||||
else
|
||||
{
|
||||
taskEpdContent[0] = "USD/MCAP";
|
||||
}
|
||||
|
||||
if (preferences.getBool("mcapBigChar", true))
|
||||
{
|
||||
firstIndex = 1;
|
||||
|
||||
priceString = priceSymbol + formatNumberWithSuffix(marketCap);
|
||||
priceString.insert(priceString.begin(), NUM_SCREENS - priceString.length(), ' ');
|
||||
}
|
||||
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++)
|
||||
{
|
||||
taskEpdContent[i] = "";
|
||||
}
|
||||
|
||||
taskEpdContent[NUM_SCREENS - groups - 1] = " $ ";
|
||||
for (uint i = 0; i < groups; i++)
|
||||
{
|
||||
taskEpdContent[(NUM_SCREENS - groups + i)] = stringValue.substr(i * 3, 3).c_str();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!(getCurrentScreen() == SCREEN_MARKET_CAP && !preferences.getBool("mcapBigChar", true)))
|
||||
{
|
||||
for (uint i = firstIndex; i < NUM_SCREENS; i++)
|
||||
{
|
||||
taskEpdContent[i] = priceString[i];
|
||||
}
|
||||
taskEpdContent = parseMarketCap(getBlockHeight(), price, priceSymbol, preferences.getBool("mcapBigChar", true));
|
||||
}
|
||||
|
||||
setEpdContent(taskEpdContent);
|
||||
@ -147,39 +69,13 @@ void workerTask(void *pvParameters)
|
||||
}
|
||||
case TASK_BLOCK_UPDATE:
|
||||
{
|
||||
std::string blockNrString = String(getBlockHeight()).c_str();
|
||||
firstIndex = 0;
|
||||
|
||||
if (getCurrentScreen() != SCREEN_HALVING_COUNTDOWN)
|
||||
{
|
||||
if (blockNrString.length() < NUM_SCREENS)
|
||||
{
|
||||
blockNrString.insert(blockNrString.begin(), NUM_SCREENS - blockNrString.length(), ' ');
|
||||
taskEpdContent[0] = "BLOCK/HEIGHT";
|
||||
firstIndex = 1;
|
||||
}
|
||||
|
||||
for (uint i = firstIndex; i < NUM_SCREENS; i++)
|
||||
{
|
||||
taskEpdContent[i] = blockNrString[i];
|
||||
}
|
||||
taskEpdContent = parseBlockHeight(getBlockHeight());
|
||||
}
|
||||
else
|
||||
{
|
||||
const uint nextHalvingBlock = 210000 - (getBlockHeight() % 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));
|
||||
taskEpdContent[0] = "BIT/COIN";
|
||||
taskEpdContent[1] = "HALV/ING";
|
||||
taskEpdContent[(NUM_SCREENS - 5)] = String(years) + "/YRS";
|
||||
taskEpdContent[(NUM_SCREENS - 4)] = String(days) + "/DAYS";
|
||||
taskEpdContent[(NUM_SCREENS - 3)] = String(hours) + "/HRS";
|
||||
taskEpdContent[(NUM_SCREENS - 2)] = String(mins) + "/MINS";
|
||||
taskEpdContent[(NUM_SCREENS - 1)] = "TO/GO";
|
||||
taskEpdContent = parseHalvingCountdown(getBlockHeight());
|
||||
}
|
||||
|
||||
if (getCurrentScreen() == SCREEN_HALVING_COUNTDOWN || getCurrentScreen() == SCREEN_BLOCK_HEIGHT)
|
||||
@ -206,7 +102,7 @@ void workerTask(void *pvParameters)
|
||||
|
||||
timeString = std::to_string(timeinfo.tm_hour) + ":" + minute.c_str();
|
||||
timeString.insert(timeString.begin(), NUM_SCREENS - timeString.length(), ' ');
|
||||
taskEpdContent[0] = String(timeinfo.tm_mday) + "/" + String(timeinfo.tm_mon + 1);
|
||||
taskEpdContent[0] = std::to_string(timeinfo.tm_mday) + "/" + std::to_string(timeinfo.tm_mon + 1);
|
||||
|
||||
for (uint i = 1; i < NUM_SCREENS; i++)
|
||||
{
|
||||
|
@ -4,6 +4,7 @@
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <data_handler.hpp>
|
||||
|
||||
#include "price_fetch.hpp"
|
||||
#include "shared.hpp"
|
||||
|
@ -45,3 +45,4 @@ struct SpiRamAllocator {
|
||||
};
|
||||
|
||||
using SpiRamJsonDocument = BasicJsonDocument<SpiRamAllocator>;
|
||||
|
||||
|
@ -313,7 +313,7 @@ void onApiSettingsPatch(AsyncWebServerRequest *request, JsonVariant &json)
|
||||
}
|
||||
}
|
||||
|
||||
String uintSettings[] = {"minSecPriceUpd", "fullRefreshMin", "ledBrightness", "mcapBigChar"};
|
||||
String uintSettings[] = {"minSecPriceUpd", "fullRefreshMin", "ledBrightness"};
|
||||
|
||||
for (String setting : uintSettings)
|
||||
{
|
||||
|
60
test/test_datahandler/test_main.cpp
Normal file
60
test/test_datahandler/test_main.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
#include <data_handler.hpp>
|
||||
#include <unity.h>
|
||||
|
||||
void setUp(void) {
|
||||
// set stuff up here
|
||||
}
|
||||
|
||||
void tearDown(void) {
|
||||
// clean stuff up here
|
||||
}
|
||||
|
||||
void test_sats_per_dollar(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());
|
||||
TEST_ASSERT_EQUAL_STRING("6", output[NUM_SCREENS-3].c_str());
|
||||
TEST_ASSERT_EQUAL_STRING("8", output[NUM_SCREENS-2].c_str());
|
||||
TEST_ASSERT_EQUAL_STRING("4", output[NUM_SCREENS-1].c_str());
|
||||
}
|
||||
|
||||
|
||||
void test_block_height_6screens(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) {
|
||||
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) {
|
||||
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) {
|
||||
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());
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
|
||||
extern "C" void app_main() {
|
||||
runUnityTests();
|
||||
}
|
Loading…
Reference in New Issue
Block a user