2019-12-10 21:21:32 +00:00
|
|
|
#include "main.h"
|
|
|
|
#include <cfenv>
|
|
|
|
#include <iostream>
|
|
|
|
|
2020-02-06 01:18:11 +00:00
|
|
|
// run ncmetric, and then change any localized decimal separator into our
|
2019-12-22 01:44:34 +00:00
|
|
|
// proud imperial yankee capitalist democratic one dot under god period.
|
|
|
|
// manifest destiny, bitchhhhhhhezzzz!
|
2020-05-17 07:27:25 +00:00
|
|
|
char* impericize_ncmetric(uintmax_t val, uintmax_t decimal, char* buf,
|
2019-12-27 22:20:20 +00:00
|
|
|
int omitdec, unsigned mult, int uprefix) {
|
|
|
|
const char* decisep = localeconv()->decimal_point;
|
|
|
|
REQUIRE(decisep);
|
|
|
|
REQUIRE(1 == strlen(decisep));
|
2020-02-06 01:18:11 +00:00
|
|
|
REQUIRE(ncmetric(val, decimal, buf, omitdec, mult, uprefix));
|
2019-12-22 01:44:34 +00:00
|
|
|
char* commie = buf;
|
2019-12-27 22:20:20 +00:00
|
|
|
while( (commie = strstr(commie, decisep)) ){
|
2019-12-22 01:44:34 +00:00
|
|
|
*commie = '.'; // https://dank.qemfd.net/images/16whcc.jpg
|
|
|
|
++commie;
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2020-05-12 22:40:14 +00:00
|
|
|
TEST_CASE("Metric") {
|
2019-12-27 22:20:20 +00:00
|
|
|
const char* decisep = localeconv()->decimal_point;
|
|
|
|
REQUIRE(decisep);
|
|
|
|
REQUIRE(1 == strlen(decisep));
|
2020-06-01 12:25:59 +00:00
|
|
|
REQUIRE(0 == fesetround(FE_TONEAREST));
|
2019-12-10 21:21:32 +00:00
|
|
|
|
2019-12-27 22:20:20 +00:00
|
|
|
SUBCASE("CornerInts") {
|
|
|
|
char buf[PREFIXSTRLEN + 1];
|
2020-02-06 01:18:11 +00:00
|
|
|
impericize_ncmetric(0, 1, buf, 0, 1000, '\0');
|
2019-12-27 22:20:20 +00:00
|
|
|
CHECK(!strcmp("0.00", buf));
|
2020-02-06 01:18:11 +00:00
|
|
|
impericize_ncmetric(0, 1, buf, 0, 1024, 'i');
|
2019-12-27 22:20:20 +00:00
|
|
|
CHECK(!strcmp("0.00", buf)); // no suffix on < mult
|
2020-02-06 01:18:11 +00:00
|
|
|
impericize_ncmetric(1, 1, buf, 0, 1000, '\0');
|
2019-12-27 22:20:20 +00:00
|
|
|
CHECK(!strcmp("1.00", buf));
|
2020-02-06 01:18:11 +00:00
|
|
|
impericize_ncmetric(1, 1, buf, 0, 1024, 'i');
|
2019-12-27 22:20:20 +00:00
|
|
|
CHECK(!strcmp("1.00", buf));
|
2020-02-06 01:18:11 +00:00
|
|
|
impericize_ncmetric(0, 1, buf, 1, 1000, '\0');
|
2019-12-27 22:20:20 +00:00
|
|
|
CHECK(!strcmp("0", buf));
|
2020-02-06 01:18:11 +00:00
|
|
|
impericize_ncmetric(0, 1, buf, 1, 1024, 'i');
|
2019-12-27 22:20:20 +00:00
|
|
|
CHECK(!strcmp("0", buf)); // no suffix on < mult
|
2020-02-06 01:18:11 +00:00
|
|
|
impericize_ncmetric(1, 1, buf, 1, 1000, '\0');
|
2019-12-27 22:20:20 +00:00
|
|
|
CHECK(!strcmp("1", buf));
|
2020-02-06 01:18:11 +00:00
|
|
|
impericize_ncmetric(1, 1, buf, 1, 1024, 'i');
|
2019-12-27 22:20:20 +00:00
|
|
|
CHECK(!strcmp("1", buf));
|
2020-02-06 01:18:11 +00:00
|
|
|
impericize_ncmetric(999, 1, buf, 1, 1000, '\0');
|
2019-12-27 22:20:20 +00:00
|
|
|
CHECK(!strcmp("999", buf));
|
2020-02-06 01:18:11 +00:00
|
|
|
impericize_ncmetric(1000, 1, buf, 1, 1000, '\0');
|
2019-12-27 22:20:20 +00:00
|
|
|
CHECK(!strcmp("1K", buf));
|
2020-02-06 01:18:11 +00:00
|
|
|
impericize_ncmetric(1000, 1, buf, 1, 1000, 'i');
|
2019-12-27 22:20:20 +00:00
|
|
|
CHECK(!strcmp("1Ki", buf));
|
2020-02-06 01:18:11 +00:00
|
|
|
impericize_ncmetric(1000, 1, buf, 1, 1024, 'i');
|
2019-12-27 22:20:20 +00:00
|
|
|
CHECK(!strcmp("1000", buf)); // FIXME should be 0.977Ki
|
2020-02-06 01:18:11 +00:00
|
|
|
impericize_ncmetric(1023, 1, buf, 1, 1000, '\0');
|
2019-12-27 22:20:20 +00:00
|
|
|
CHECK(!strcmp("1.02K", buf));
|
2020-02-06 01:18:11 +00:00
|
|
|
impericize_ncmetric(1023, 1, buf, 1, 1024, 'i');
|
2019-12-27 22:20:20 +00:00
|
|
|
CHECK(!strcmp("1023", buf));
|
2020-02-06 01:18:11 +00:00
|
|
|
impericize_ncmetric(1024, 1, buf, 1, 1000, '\0');
|
2019-12-27 22:20:20 +00:00
|
|
|
CHECK(!strcmp("1.02K", buf));
|
2020-02-06 01:18:11 +00:00
|
|
|
impericize_ncmetric(1024, 1, buf, 1, 1024, 'i');
|
2019-12-27 22:20:20 +00:00
|
|
|
CHECK(!strcmp("1Ki", buf));
|
2020-02-06 01:18:11 +00:00
|
|
|
impericize_ncmetric(1025, 1, buf, 1, 1000, '\0');
|
2019-12-27 22:20:20 +00:00
|
|
|
CHECK(!strcmp("1.02K", buf));
|
2020-02-06 01:18:11 +00:00
|
|
|
impericize_ncmetric(1025, 1, buf, 0, 1024, 'i');
|
2019-12-27 22:20:20 +00:00
|
|
|
CHECK(!strcmp("1.00Ki", buf));
|
2020-02-06 01:18:11 +00:00
|
|
|
impericize_ncmetric(1025, 1, buf, 1, 1024, 'i');
|
2019-12-27 22:20:20 +00:00
|
|
|
CHECK(!strcmp("1.00Ki", buf));
|
2020-02-06 01:18:11 +00:00
|
|
|
impericize_ncmetric(4096, 1, buf, 1, 1000, '\0');
|
2020-06-01 12:25:59 +00:00
|
|
|
CHECK(!strcmp("4.10K", buf));
|
2020-11-28 22:08:45 +00:00
|
|
|
impericize_ncmetric(4096, 1, buf, 0, 1024, 'i');
|
|
|
|
CHECK(!strcmp("4.00Ki", buf));
|
2020-02-06 01:18:11 +00:00
|
|
|
impericize_ncmetric(4096, 1, buf, 1, 1024, 'i');
|
2019-12-27 22:20:20 +00:00
|
|
|
CHECK(!strcmp("4Ki", buf));
|
|
|
|
}
|
2019-12-10 21:21:32 +00:00
|
|
|
|
2019-12-27 22:20:20 +00:00
|
|
|
SUBCASE("Maxints") {
|
|
|
|
char buf[PREFIXSTRLEN + 1];
|
|
|
|
// FIXME these will change based on the size of intmax_t and uintmax_t
|
2020-02-06 01:18:11 +00:00
|
|
|
impericize_ncmetric(INTMAX_MAX - 1, 1, buf, 0, 1000, '\0');
|
2019-12-27 22:20:20 +00:00
|
|
|
CHECK(!strcmp("9.22E", buf));
|
2020-02-06 01:18:11 +00:00
|
|
|
impericize_ncmetric(INTMAX_MAX, 1, buf, 0, 1000, '\0');
|
2019-12-27 22:20:20 +00:00
|
|
|
CHECK(!strcmp("9.22E", buf));
|
2020-02-06 01:18:11 +00:00
|
|
|
impericize_ncmetric(UINTMAX_MAX - 1, 1, buf, 0, 1000, '\0');
|
2020-06-01 12:25:59 +00:00
|
|
|
CHECK(!strcmp("18.45E", buf));
|
2020-02-06 01:18:11 +00:00
|
|
|
impericize_ncmetric(UINTMAX_MAX, 1, buf, 0, 1000, '\0');
|
2020-06-01 12:25:59 +00:00
|
|
|
CHECK(!strcmp("18.45E", buf));
|
2019-12-27 22:20:20 +00:00
|
|
|
}
|
2019-12-10 21:21:32 +00:00
|
|
|
|
2019-12-27 22:20:20 +00:00
|
|
|
SUBCASE("Maxints1024") {
|
|
|
|
char buf[PREFIXSTRLEN + 1], gold[PREFIXSTRLEN + 1];
|
|
|
|
// FIXME these will change based on the size of intmax_t and uintmax_t
|
2020-06-01 12:25:59 +00:00
|
|
|
REQUIRE(ncmetric(((double)(INTMAX_MAX - 1ull)), 1, buf, 0, 1024, 'i'));
|
|
|
|
sprintf(gold, "%.2fEi", ((double)(INTMAX_MAX - 1ull)) / (1ull << 60));
|
|
|
|
CHECK(!strcmp(gold, buf));
|
|
|
|
REQUIRE(ncmetric(((double)(INTMAX_MAX - (1ull << 53))), 1, buf, 0, 1024, 'i'));
|
2019-12-27 22:20:20 +00:00
|
|
|
sprintf(gold, "%.2fEi", ((double)(INTMAX_MAX - (1ull << 53))) / (1ull << 60));
|
|
|
|
CHECK(!strcmp(gold, buf));
|
2020-05-13 14:50:42 +00:00
|
|
|
REQUIRE(ncmetric(INTMAX_MAX + 1ull, 1, buf, 0, 1024, 'i'));
|
2019-12-27 22:20:20 +00:00
|
|
|
sprintf(gold, "%.2fEi", ((double)(INTMAX_MAX + 1ull)) / (1ull << 60));
|
|
|
|
CHECK(!strcmp(gold, buf));
|
2020-02-06 01:18:11 +00:00
|
|
|
impericize_ncmetric(UINTMAX_MAX - 1, 1, buf, 0, 1024, 'i');
|
2020-06-01 12:25:59 +00:00
|
|
|
CHECK(!strcmp("16.00Ei", buf));
|
2020-02-06 01:18:11 +00:00
|
|
|
impericize_ncmetric(UINTMAX_MAX, 1, buf, 0, 1024, 'i');
|
2020-06-01 12:25:59 +00:00
|
|
|
CHECK(!strcmp("16.00Ei", buf));
|
2020-02-06 01:18:11 +00:00
|
|
|
ncmetric(UINTMAX_MAX - (1ull << 53), 1, buf, 0, 1024, 'i');
|
2019-12-27 22:20:20 +00:00
|
|
|
sprintf(gold, "%.2fEi", ((double)UINTMAX_MAX - (1ull << 53)) / (1ull << 60));
|
|
|
|
CHECK(!strcmp(gold, buf));
|
|
|
|
}
|
2019-12-10 21:21:32 +00:00
|
|
|
|
2019-12-27 22:20:20 +00:00
|
|
|
const char suffixes[] = "\0KMGTPE";
|
2019-12-10 21:21:32 +00:00
|
|
|
|
2019-12-27 22:20:20 +00:00
|
|
|
SUBCASE("PowersOfTen") {
|
|
|
|
char gold[PREFIXSTRLEN + 1];
|
|
|
|
char buf[PREFIXSTRLEN + 1];
|
|
|
|
uintmax_t goldval = 1;
|
|
|
|
uintmax_t val = 1;
|
|
|
|
size_t i = 0;
|
|
|
|
do{
|
2020-02-06 01:18:11 +00:00
|
|
|
ncmetric(val, 1, buf, 0, 1000, '\0');
|
2019-12-27 22:20:20 +00:00
|
|
|
const int sidx = i / 3;
|
|
|
|
snprintf(gold, sizeof(gold), "%ju%s00%c", goldval, decisep, suffixes[sidx]);
|
|
|
|
CHECK(!strcmp(gold, buf));
|
|
|
|
if(UINTMAX_MAX / val < 10){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
val *= 10;
|
|
|
|
if((goldval *= 10) == 1000){
|
|
|
|
goldval = 1;
|
|
|
|
}
|
|
|
|
}while(++i < sizeof(suffixes) * 3);
|
|
|
|
// If we ran through all our suffixes, that's a problem
|
|
|
|
CHECK(sizeof(suffixes) * 3 > i);
|
|
|
|
}
|
2019-12-10 21:21:32 +00:00
|
|
|
|
2019-12-27 22:20:20 +00:00
|
|
|
SUBCASE("PowersOfTenNoDec") {
|
|
|
|
char gold[PREFIXSTRLEN + 1];
|
|
|
|
char buf[PREFIXSTRLEN + 1];
|
|
|
|
uintmax_t goldval = 1;
|
|
|
|
uintmax_t val = 1;
|
|
|
|
size_t i = 0;
|
|
|
|
do{
|
2020-02-06 01:18:11 +00:00
|
|
|
ncmetric(val, 1, buf, 1, 1000, '\0');
|
2019-12-27 22:20:20 +00:00
|
|
|
const int sidx = i / 3;
|
|
|
|
snprintf(gold, sizeof(gold), "%ju%c", goldval, suffixes[sidx]);
|
|
|
|
CHECK(!strcmp(gold, buf));
|
|
|
|
if(UINTMAX_MAX / val < 10){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
val *= 10;
|
|
|
|
if((goldval *= 10) == 1000){
|
|
|
|
goldval = 1;
|
|
|
|
}
|
|
|
|
}while(++i < sizeof(suffixes) * 3);
|
|
|
|
// If we ran through all our suffixes, that's a problem
|
|
|
|
CHECK(sizeof(suffixes) * 3 > i);
|
|
|
|
}
|
2019-12-10 21:21:32 +00:00
|
|
|
|
2019-12-27 22:20:20 +00:00
|
|
|
SUBCASE("PowersOfTwo") {
|
|
|
|
char gold[BPREFIXSTRLEN + 1];
|
|
|
|
char buf[BPREFIXSTRLEN + 1];
|
|
|
|
uintmax_t goldval = 1;
|
|
|
|
uintmax_t val = 1;
|
|
|
|
size_t i = 0;
|
|
|
|
do{
|
2020-02-06 01:18:11 +00:00
|
|
|
ncmetric(val, 1, buf, 0, 1024, 'i');
|
2019-12-27 22:20:20 +00:00
|
|
|
const int sidx = i / 10;
|
|
|
|
snprintf(gold, sizeof(gold), "%ju%s00%ci", goldval, decisep, suffixes[sidx]);
|
|
|
|
CHECK(!strcmp(gold, buf));
|
|
|
|
if(UINTMAX_MAX / val < 10){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
val *= 2;
|
|
|
|
if((goldval *= 2) == 1024){
|
|
|
|
goldval = 1;
|
|
|
|
}
|
|
|
|
}while(++i < sizeof(suffixes) * 10);
|
|
|
|
// If we ran through all our suffixes, that's a problem
|
|
|
|
CHECK(sizeof(suffixes) * 10 > i);
|
|
|
|
}
|
2019-12-10 21:21:32 +00:00
|
|
|
|
2019-12-27 22:20:20 +00:00
|
|
|
SUBCASE("PowersOfTwoNoDec") {
|
|
|
|
char gold[BPREFIXSTRLEN + 1];
|
|
|
|
char buf[BPREFIXSTRLEN + 1];
|
|
|
|
uintmax_t goldval = 1;
|
|
|
|
uintmax_t val = 1;
|
|
|
|
size_t i = 0;
|
|
|
|
do{
|
2020-02-06 01:18:11 +00:00
|
|
|
ncmetric(val, 1, buf, 1, 1024, 'i');
|
2019-12-27 22:20:20 +00:00
|
|
|
const int sidx = i / 10;
|
|
|
|
snprintf(gold, sizeof(gold), "%ju%ci", goldval, suffixes[sidx]);
|
|
|
|
CHECK(!strcmp(gold, buf));
|
|
|
|
if(UINTMAX_MAX / val < 10){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
val *= 2;
|
|
|
|
if((goldval *= 2) == 1024){
|
|
|
|
goldval = 1;
|
|
|
|
}
|
|
|
|
}while(++i < sizeof(suffixes) * 10);
|
|
|
|
// If we ran through all our suffixes, that's a problem
|
|
|
|
CHECK(sizeof(suffixes) * 10 > i);
|
|
|
|
}
|
2019-12-10 21:21:32 +00:00
|
|
|
|
2019-12-27 22:20:20 +00:00
|
|
|
SUBCASE("PowersOfTwoAsTens") {
|
|
|
|
char gold[PREFIXSTRLEN + 1];
|
|
|
|
char buf[PREFIXSTRLEN + 1];
|
|
|
|
uintmax_t vfloor = 1;
|
|
|
|
uintmax_t val = 1;
|
|
|
|
size_t i = 0;
|
|
|
|
do{
|
2020-02-06 01:18:11 +00:00
|
|
|
ncmetric(val, 1, buf, 0, 1000, '\0');
|
2019-12-27 22:20:20 +00:00
|
|
|
const int sidx = i / 10;
|
2020-06-01 08:24:04 +00:00
|
|
|
snprintf(gold, sizeof(gold), "%.2f%c", ((double)val) / vfloor, suffixes[sidx]);
|
2019-12-27 22:20:20 +00:00
|
|
|
CHECK(!strcmp(gold, buf));
|
|
|
|
if(UINTMAX_MAX / val < 10){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
val *= 2;
|
|
|
|
if(i % 10 == 9){
|
|
|
|
vfloor *= 1000;
|
|
|
|
}
|
|
|
|
}while(++i < sizeof(suffixes) * 10);
|
|
|
|
// If we ran through all our suffixes, that's a problem
|
|
|
|
CHECK(sizeof(suffixes) * 10 > i);
|
|
|
|
}
|
2019-12-10 21:21:32 +00:00
|
|
|
|
2019-12-27 22:20:20 +00:00
|
|
|
SUBCASE("PowersOfTenAsTwos") {
|
|
|
|
char gold[BPREFIXSTRLEN + 1];
|
|
|
|
char buf[BPREFIXSTRLEN + 1];
|
|
|
|
uintmax_t vfloor = 1;
|
|
|
|
uintmax_t val = 1;
|
|
|
|
size_t i = 0;
|
|
|
|
do{
|
2020-02-06 01:18:11 +00:00
|
|
|
ncmetric(val, 1, buf, 0, 1024, 'i');
|
2019-12-27 22:20:20 +00:00
|
|
|
const int sidx = i ? (i - 1) / 3 : 0;
|
2020-06-01 08:24:04 +00:00
|
|
|
snprintf(gold, sizeof(gold), "%.2f%ci", ((double)val) / vfloor, suffixes[sidx]);
|
2019-12-27 22:20:20 +00:00
|
|
|
CHECK(!strcmp(gold, buf));
|
|
|
|
if(UINTMAX_MAX / val < 10){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
val *= 10;
|
|
|
|
if(i && i % 3 == 0){
|
|
|
|
vfloor *= 1024;
|
|
|
|
}
|
|
|
|
}while(++i < sizeof(suffixes) * 10);
|
|
|
|
// If we ran through all our suffixes, that's a problem
|
|
|
|
CHECK(sizeof(suffixes) * 10 > i);
|
|
|
|
}
|
2019-12-10 21:21:32 +00:00
|
|
|
|
2019-12-27 22:20:20 +00:00
|
|
|
SUBCASE("PowersOfTenMinusOne") {
|
|
|
|
char gold[PREFIXSTRLEN + 1];
|
|
|
|
char buf[PREFIXSTRLEN + 1];
|
|
|
|
uintmax_t vfloor = 1;
|
|
|
|
uintmax_t val = 1;
|
|
|
|
size_t i = 0;
|
|
|
|
do{
|
2020-02-06 01:18:11 +00:00
|
|
|
ncmetric(val - 1, 1, buf, 0, 1000, '\0');
|
2019-12-27 22:20:20 +00:00
|
|
|
const int sidx = i ? (i - 1) / 3 : 0;
|
2020-06-01 08:24:04 +00:00
|
|
|
snprintf(gold, sizeof(gold), "%.2f%c", ((double)(val - 1)) / vfloor, suffixes[sidx]);
|
2019-12-27 22:20:20 +00:00
|
|
|
CHECK(!strcmp(gold, buf));
|
|
|
|
if(UINTMAX_MAX / val < 10){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
val *= 10;
|
|
|
|
if(i && i % 3 == 0){
|
|
|
|
vfloor *= 1000;
|
|
|
|
}
|
|
|
|
}while(++i < sizeof(suffixes) * 3);
|
|
|
|
// If we ran through all our suffixes, that's a problem
|
|
|
|
CHECK(sizeof(suffixes) * 3 > i);
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("PowersOfTenPlusOne") {
|
|
|
|
char gold[PREFIXSTRLEN + 1];
|
|
|
|
char buf[PREFIXSTRLEN + 1];
|
|
|
|
uintmax_t vfloor = 1;
|
|
|
|
uintmax_t val = 1;
|
|
|
|
size_t i = 0;
|
|
|
|
do{
|
2020-02-06 01:18:11 +00:00
|
|
|
ncmetric(val + 1, 1, buf, 0, 1000, '\0');
|
2019-12-27 22:20:20 +00:00
|
|
|
const int sidx = i / 3;
|
2020-06-01 08:24:04 +00:00
|
|
|
snprintf(gold, sizeof(gold), "%.2f%c", ((double)(val + 1)) / vfloor, suffixes[sidx]);
|
2019-12-27 22:20:20 +00:00
|
|
|
CHECK(!strcmp(gold, buf));
|
|
|
|
if(UINTMAX_MAX / val < 10){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
val *= 10;
|
|
|
|
if(i % 3 == 2){
|
|
|
|
vfloor *= 1000;
|
|
|
|
}
|
|
|
|
}while(++i < sizeof(suffixes) * 3);
|
|
|
|
// If we ran through all our suffixes, that's a problem
|
|
|
|
CHECK(sizeof(suffixes) * 3 > i);
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("PowersOfTenMinusOneAsTwos") {
|
|
|
|
char gold[BPREFIXSTRLEN + 1];
|
|
|
|
char buf[BPREFIXSTRLEN + 1];
|
|
|
|
uintmax_t vfloor = 1;
|
|
|
|
uintmax_t val = 1;
|
|
|
|
size_t i = 0;
|
|
|
|
do{
|
2020-02-06 01:18:11 +00:00
|
|
|
ncmetric(val - 1, 1, buf, 0, 1024, 'i');
|
2019-12-27 22:20:20 +00:00
|
|
|
const int sidx = i ? (i - 1) / 3 : 0;
|
2020-06-01 08:24:04 +00:00
|
|
|
snprintf(gold, sizeof(gold), "%.2f%ci", ((double)(val - 1)) / vfloor, suffixes[sidx]);
|
2019-12-27 22:20:20 +00:00
|
|
|
CHECK(!strcmp(gold, buf));
|
|
|
|
if(UINTMAX_MAX / val < 10){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
val *= 10;
|
|
|
|
if(i && i % 3 == 0){
|
|
|
|
vfloor *= 1024;
|
|
|
|
}
|
|
|
|
}while(++i < sizeof(suffixes) * 10);
|
|
|
|
// If we ran through all our suffixes, that's a problem
|
|
|
|
CHECK(sizeof(suffixes) * 10 > i);
|
|
|
|
}
|
2019-12-10 21:21:32 +00:00
|
|
|
|
2020-05-17 07:27:25 +00:00
|
|
|
constexpr auto GIG = 1000000000ull;
|
|
|
|
|
2020-01-06 20:58:16 +00:00
|
|
|
// Output ought be scaled down for output while maintaining precision during
|
|
|
|
// computation of that output. For instance, we might feed a number of
|
|
|
|
// nanoseconds, but want output in seconds.
|
2020-05-17 07:27:25 +00:00
|
|
|
// This requires 'decimal' = GIG.
|
2020-01-07 05:24:20 +00:00
|
|
|
SUBCASE("ScaledGigSupra") {
|
2020-05-29 06:24:41 +00:00
|
|
|
char gold[PREFIXSTRLEN + 1];
|
|
|
|
snprintf(gold, sizeof(gold), "%.2f", 9.029); // 9.02
|
2020-01-06 20:58:16 +00:00
|
|
|
char buf[PREFIXSTRLEN + 1];
|
|
|
|
uintmax_t val = 9027854993;
|
2020-05-17 07:27:25 +00:00
|
|
|
uintmax_t decimal = GIG;
|
2020-01-06 20:58:16 +00:00
|
|
|
REQUIRE(qprefix(val, decimal, buf, 0));
|
|
|
|
CHECK(!strcmp(buf, gold));
|
|
|
|
}
|
|
|
|
|
2020-01-07 05:24:20 +00:00
|
|
|
SUBCASE("ScaledGigUnity") {
|
2020-05-29 06:24:41 +00:00
|
|
|
char gold[PREFIXSTRLEN + 1];
|
|
|
|
snprintf(gold, sizeof(gold), "%.2f", 1.0); // 1.00
|
2020-01-07 05:24:20 +00:00
|
|
|
char buf[PREFIXSTRLEN + 1];
|
2020-05-17 07:27:25 +00:00
|
|
|
uintmax_t decimal = GIG;
|
2020-01-07 05:24:20 +00:00
|
|
|
uintmax_t val = decimal;
|
|
|
|
REQUIRE(qprefix(val, decimal, buf, 0));
|
|
|
|
CHECK(!strcmp(buf, gold));
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("ScaledGigJustAbove") {
|
2020-05-29 06:24:41 +00:00
|
|
|
char gold[PREFIXSTRLEN + 1];
|
|
|
|
snprintf(gold, sizeof(gold), "%.2f", 1.0); // 1.00
|
2020-01-07 05:24:20 +00:00
|
|
|
char buf[PREFIXSTRLEN + 1];
|
|
|
|
uintmax_t val = 1000000001;
|
2020-05-17 07:27:25 +00:00
|
|
|
uintmax_t decimal = GIG;
|
2020-01-07 05:24:20 +00:00
|
|
|
REQUIRE(qprefix(val, decimal, buf, 0));
|
|
|
|
CHECK(!strcmp(buf, gold));
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("ScaledGigJustBelow") {
|
2020-05-29 06:24:41 +00:00
|
|
|
char gold[PREFIXSTRLEN + 1];
|
|
|
|
snprintf(gold, sizeof(gold), "%.2fm", 999.999); //999.99
|
2020-01-07 05:24:20 +00:00
|
|
|
char buf[PREFIXSTRLEN + 1];
|
|
|
|
uintmax_t val = 999999999;
|
2020-05-17 07:27:25 +00:00
|
|
|
uintmax_t decimal = GIG;
|
2020-01-07 05:24:20 +00:00
|
|
|
REQUIRE(qprefix(val, decimal, buf, 0));
|
|
|
|
CHECK(!strcmp(buf, gold));
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("ScaledGigSub") {
|
2020-05-29 06:24:41 +00:00
|
|
|
char gold[PREFIXSTRLEN + 1];
|
2020-06-01 12:25:59 +00:00
|
|
|
snprintf(gold, sizeof(gold), "%.2fm", 27.85); // 27.85
|
2020-01-07 05:24:20 +00:00
|
|
|
char buf[PREFIXSTRLEN + 1];
|
|
|
|
uintmax_t val = 27854993;
|
2020-05-17 07:27:25 +00:00
|
|
|
uintmax_t decimal = GIG;
|
2020-01-07 05:24:20 +00:00
|
|
|
REQUIRE(qprefix(val, decimal, buf, 0));
|
|
|
|
CHECK(!strcmp(buf, gold));
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("ScaledGigSubSub") {
|
2020-05-29 06:24:41 +00:00
|
|
|
char gold[PREFIXSTRLEN + 1];
|
2020-06-01 12:25:59 +00:00
|
|
|
snprintf(gold, sizeof(gold), "%.2fm", 7.85); // 7.85
|
2020-01-07 05:24:20 +00:00
|
|
|
char buf[PREFIXSTRLEN + 1];
|
|
|
|
uintmax_t val = 7854993;
|
2020-05-17 07:27:25 +00:00
|
|
|
uintmax_t decimal = GIG;
|
2020-01-07 05:24:20 +00:00
|
|
|
REQUIRE(qprefix(val, decimal, buf, 0));
|
|
|
|
CHECK(!strcmp(buf, gold));
|
|
|
|
}
|
|
|
|
|
2020-05-17 07:27:25 +00:00
|
|
|
SUBCASE("SmallCorners") {
|
|
|
|
char buf[PREFIXSTRLEN + 1];
|
|
|
|
impericize_ncmetric(1, 1000, buf, 0, 1000, '\0');
|
|
|
|
CHECK(!strcmp("1.00m", buf));
|
|
|
|
impericize_ncmetric(1, 1024, buf, 0, 1024, 'i');
|
|
|
|
CHECK(!strcmp("1.00mi", buf));
|
|
|
|
|
|
|
|
impericize_ncmetric(1, 1000000000000000000ull, buf, 0, 1000, '\0');
|
|
|
|
CHECK(!strcmp("1.00a", buf));
|
|
|
|
|
2020-05-17 08:16:02 +00:00
|
|
|
impericize_ncmetric(19, 10000000000000000ull, buf, 0, 1000, '\0');
|
2020-06-01 12:25:59 +00:00
|
|
|
CHECK(!strcmp("1.90f", buf));
|
2020-05-17 08:16:02 +00:00
|
|
|
impericize_ncmetric(99, 10000000000000000ull, buf, 0, 1000, '\0');
|
2020-06-01 12:25:59 +00:00
|
|
|
CHECK(!strcmp("9.90f", buf));
|
2020-05-17 07:27:25 +00:00
|
|
|
|
2020-05-17 08:16:02 +00:00
|
|
|
impericize_ncmetric(100, 10000000000000000ull, buf, 0, 1000, '\0');
|
|
|
|
CHECK(!strcmp("10.00f", buf));
|
2020-05-17 07:27:25 +00:00
|
|
|
|
|
|
|
impericize_ncmetric(1, 10, buf, 0, 1000, '\0');
|
|
|
|
CHECK(!strcmp("100.00m", buf));
|
|
|
|
impericize_ncmetric(1, 100, buf, 0, 1000, '\0');
|
|
|
|
CHECK(!strcmp("10.00m", buf));
|
|
|
|
impericize_ncmetric(10, 100, buf, 0, 1000, '\0');
|
|
|
|
CHECK(!strcmp("100.00m", buf));
|
|
|
|
impericize_ncmetric(10, 1000, buf, 0, 1000, '\0');
|
|
|
|
CHECK(!strcmp("10.00m", buf));
|
|
|
|
impericize_ncmetric(100, 1000, buf, 0, 1000, '\0');
|
|
|
|
CHECK(!strcmp("100.00m", buf));
|
|
|
|
impericize_ncmetric(1000, 1000, buf, 0, 1000, '\0');
|
|
|
|
CHECK(!strcmp("1.00", buf));
|
|
|
|
|
|
|
|
impericize_ncmetric(100, 1000000000000000ull, buf, 0, 1000, '\0');
|
|
|
|
CHECK(!strcmp("100.00f", buf));
|
|
|
|
|
|
|
|
impericize_ncmetric(100, 1000000000000ull, buf, 0, 1000, '\0');
|
|
|
|
CHECK(!strcmp("100.00p", buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("NegativePowersOfTen") {
|
2020-11-29 04:15:07 +00:00
|
|
|
auto nc_ = testing_notcurses();
|
|
|
|
if(!nc_){
|
|
|
|
return;
|
|
|
|
}
|
2020-07-04 09:46:01 +00:00
|
|
|
const wchar_t* smallsuffixes;
|
2020-11-29 04:15:07 +00:00
|
|
|
if(notcurses_canutf8(nc_)){
|
2020-07-04 09:46:01 +00:00
|
|
|
smallsuffixes = L"yzafpnµm";
|
|
|
|
}else{
|
|
|
|
smallsuffixes = L"yzafpnum";
|
|
|
|
}
|
2020-05-17 07:27:25 +00:00
|
|
|
char gold[PREFIXSTRLEN + 1];
|
|
|
|
char buf[PREFIXSTRLEN + 1];
|
|
|
|
uintmax_t goldval = 1;
|
|
|
|
uintmax_t val = 1;
|
|
|
|
size_t i = 0;
|
|
|
|
do{
|
2020-05-17 08:16:02 +00:00
|
|
|
REQUIRE(qprefix(val, 1000000000000000ull, buf, '\0'));
|
|
|
|
const int sidx = i / 3 + 3;
|
2020-05-17 07:27:25 +00:00
|
|
|
snprintf(gold, sizeof(gold), "%ju%s00%lc", goldval, decisep, smallsuffixes[sidx]);
|
|
|
|
CHECK(!strcmp(gold, buf));
|
|
|
|
if(UINTMAX_MAX / val < 10){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
val *= 10;
|
|
|
|
if((goldval *= 10) == 1000){
|
|
|
|
goldval = 1;
|
|
|
|
}
|
2020-05-17 08:16:02 +00:00
|
|
|
}while(++i < (wcslen(smallsuffixes) - 3) * 3);
|
2020-05-17 07:27:25 +00:00
|
|
|
// If we ran through all our suffixes, that's a problem
|
2020-07-04 09:46:01 +00:00
|
|
|
CHECK(wcslen(smallsuffixes) * 3 > i);
|
2020-12-07 03:55:13 +00:00
|
|
|
CHECK(0 == notcurses_stop(nc_));
|
2020-05-17 07:27:25 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 21:34:35 +00:00
|
|
|
// inspired by #929
|
|
|
|
SUBCASE("BigMult") {
|
|
|
|
char qbuf[IPREFIXSTRLEN + 1];
|
|
|
|
CHECK(nullptr != qprefix(1115614, 1000, qbuf, '\0'));
|
|
|
|
CHECK(0 == strcmp("1.11K", qbuf));
|
|
|
|
CHECK(nullptr != iprefix(372688, 1024, qbuf, '\0'));
|
|
|
|
CHECK(0 == strcmp("363.95", qbuf));
|
|
|
|
CHECK(nullptr != iprefix(372688, 1, qbuf, '\0'));
|
|
|
|
CHECK(0 == strcmp("363.95K", qbuf));
|
|
|
|
}
|
|
|
|
|
2019-12-10 21:21:32 +00:00
|
|
|
}
|