upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/selftest.cpp
diff options
context:
space:
mode:
authorBitflash <admin@bitflash.network>2026-08-11 10:13:04 -0300
committerBitflash <admin@bitflash.network>2026-08-11 10:13:04 -0300
commitfaf6c4d24304b770c2d43515da04832b2124c92a (patch)
treee2ec970053c813bcb330f0913953f030ecc6d19e /src/selftest.cpp
parent3442c1c7f4b0476840b4ab3fab244eb000be0d04 (diff)
money: ParseMoney accepts full 8 decimals (0.5, 0.00000001)
The legacy parser only accepted two decimal places (cents) and rejected 0.5, leaving six of the eight coin decimals unreachable by -sendto and the send window. Parse up to 8 fractional digits Bitcoin-style, reject >8 to avoid silent truncation. Adds the parse-money selftest (12 cases).
Diffstat (limited to 'src/selftest.cpp')
-rw-r--r--src/selftest.cpp37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/selftest.cpp b/src/selftest.cpp
index 69a3596..dcb9192 100644
--- a/src/selftest.cpp
+++ b/src/selftest.cpp
@@ -1218,6 +1218,41 @@ static int RunConsensusLimitsSelfTest()
1218 return nFail == 0 ? 0 : 1; 1218 return nFail == 0 ? 0 : 1;
1219} 1219}
1220 1220
1221static int RunParseMoneySelfTest()
1222{
1223 printf("parse-money self-test\n");
1224 int nFail = 0;
1225 struct Case { const char* in; bool ok; int64 val; };
1226 Case cases[] = {
1227 {"0.5", true, 50000000LL},
1228 {"1", true, 100000000LL},
1229 {"1.23456789", true, 123456789LL},
1230 {"0.00000001", true, 1LL},
1231 {"10.00000000", true, 1000000000LL},
1232 {"1,000", true, 100000000000LL},
1233 {"1.5 ", true, 150000000LL},
1234 {"0.123456789", false, 0LL},
1235 {"1.2.3", false, 0LL},
1236 {"abc", false, 0LL},
1237 {"-1", false, 0LL},
1238 {"1e5", false, 0LL},
1239 };
1240 for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++)
1241 {
1242 int64 v = -1;
1243 bool r = ParseMoney(cases[i].in, v);
1244 bool pass = (r == cases[i].ok) && (!r || v == cases[i].val);
1245 char desc[160];
1246 snprintf(desc, sizeof(desc), "ParseMoney(\"%s\") %s and value matches",
1247 cases[i].in, cases[i].ok ? "accepts" : "rejects");
1248 nFail += Check(pass, desc) ? 0 : 1;
1249 }
1250 printf("%s (%d failure%s)\n", nFail == 0 ? "ALL TESTS PASSED" : "TESTS FAILED",
1251 nFail, nFail == 1 ? "" : "s");
1252 fflush(stdout);
1253 return nFail == 0 ? 0 : 1;
1254}
1255
1221int RunSelfTest(const std::string& name) 1256int RunSelfTest(const std::string& name)
1222{ 1257{
1223 AttachTerminal(); 1258 AttachTerminal();
@@ -1238,6 +1273,8 @@ int RunSelfTest(const std::string& name)
1238 return RunConsensusLimitsSelfTest(); 1273 return RunConsensusLimitsSelfTest();
1239 if (name == "pool-stratum") 1274 if (name == "pool-stratum")
1240 return RunPoolStratumSelfTest(); 1275 return RunPoolStratumSelfTest();
1276 if (name == "parse-money")
1277 return RunParseMoneySelfTest();
1241 1278
1242 printf("Unknown self-test '%s'\n", name.c_str()); 1279 printf("Unknown self-test '%s'\n", name.c_str());
1243 printf("Known self-tests: wallet-keypool, wallet-hd, wallet-format, wallet-crypto, wallet-encrypt, net-message, consensus-limits, pool-stratum\n"); 1280 printf("Known self-tests: wallet-keypool, wallet-hd, wallet-format, wallet-crypto, wallet-encrypt, net-message, consensus-limits, pool-stratum\n");