Skip to content

Commit

Permalink
Exclude more lines from test coverage
Browse files Browse the repository at this point in the history
These fall into a few categories:
- `_unreachable()`
- Verbose print messages
- Errors that should never practically occur (alloc/read/write failure,
  more than UINT32_MAX anonymous labels, etc)
  • Loading branch information
Rangi42 committed Feb 17, 2025
1 parent 632342b commit f84c2d6
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 10 deletions.
34 changes: 30 additions & 4 deletions src/fix/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,10 +698,12 @@ static char const *mbcName(MbcType type) {
case MBC_WRONG_FEATURES:
case MBC_BAD_RANGE:
case MBC_BAD_TPP1:
// LCOV_EXCL_START
unreachable_();
}

unreachable_();
// LCOV_EXCL_STOP
}

static bool hasRAM(MbcType type) {
Expand Down Expand Up @@ -763,7 +765,7 @@ static bool hasRAM(MbcType type) {
break;
}

unreachable_();
unreachable_(); // LCOV_EXCL_LINE
}

static uint8_t const nintendoLogo[] = {
Expand Down Expand Up @@ -814,7 +816,7 @@ static ssize_t readBytes(int fd, uint8_t *buf, size_t len) {
ssize_t ret = read(fd, buf, len);

if (ret == -1 && errno != EINTR) { // Return errors, unless we only were interrupted
return -1;
return -1; // LCOV_EXCL_LINE
}
// EOF reached
if (ret == 0) {
Expand All @@ -841,7 +843,7 @@ static ssize_t writeBytes(int fd, uint8_t *buf, size_t len) {
ssize_t ret = write(fd, buf, len);

if (ret == -1 && errno != EINTR) { // Return errors, unless we only were interrupted
return -1;
return -1; // LCOV_EXCL_LINE
}
// If anything was written, accumulate it, and continue
if (ret != -1) {
Expand Down Expand Up @@ -895,8 +897,10 @@ static void processFile(int input, int output, char const *name, off_t fileSize)
ssize_t headerSize = (cartridgeType & 0xFF00) == TPP1 ? 0x154 : 0x150;

if (rom0Len == -1) {
// LCOV_EXCL_START
report("FATAL: Failed to read \"%s\"'s header: %s\n", name, strerror(errno));
return;
// LCOV_EXCL_STOP
} else if (rom0Len < headerSize) {
report(
"FATAL: \"%s\" too short, expected at least %jd ($%jx) bytes, got only %jd\n",
Expand Down Expand Up @@ -1134,8 +1138,10 @@ static void processFile(int input, int output, char const *name, off_t fileSize)
// write the header
if (input == output) {
if (lseek(output, 0, SEEK_SET) == static_cast<off_t>(-1)) {
// LCOV_EXCL_START
report("FATAL: Failed to rewind \"%s\": %s\n", name, strerror(errno));
return;
// LCOV_EXCL_STOP
}
// If modifying the file in-place, we only need to edit the header
// However, padding may have modified ROM0 (added padding), so don't in that case
Expand All @@ -1146,16 +1152,20 @@ static void processFile(int input, int output, char const *name, off_t fileSize)
writeLen = writeBytes(output, rom0, rom0Len);

if (writeLen == -1) {
// LCOV_EXCL_START
report("FATAL: Failed to write \"%s\"'s ROM0: %s\n", name, strerror(errno));
return;
// LCOV_EXCL_STOP
} else if (writeLen < rom0Len) {
// LCOV_EXCL_START
report(
"FATAL: Could only write %jd of \"%s\"'s %jd ROM0 bytes\n",
static_cast<intmax_t>(writeLen),
name,
static_cast<intmax_t>(rom0Len)
);
return;
// LCOV_EXCL_STOP
}

// Output ROMX if it was buffered
Expand All @@ -1164,25 +1174,31 @@ static void processFile(int input, int output, char const *name, off_t fileSize)
// so it's fine to cast to `size_t`
writeLen = writeBytes(output, romx.data(), totalRomxLen);
if (writeLen == -1) {
// LCOV_EXCL_START
report("FATAL: Failed to write \"%s\"'s ROMX: %s\n", name, strerror(errno));
return;
// LCOV_EXCL_STOP
} else if (static_cast<size_t>(writeLen) < totalRomxLen) {
// LCOV_EXCL_START
report(
"FATAL: Could only write %jd of \"%s\"'s %zu ROMX bytes\n",
static_cast<intmax_t>(writeLen),
name,
totalRomxLen
);
return;
// LCOV_EXCL_STOP
}
}

// Output padding
if (padValue != UNSPECIFIED) {
if (input == output) {
if (lseek(output, 0, SEEK_END) == static_cast<off_t>(-1)) {
// LCOV_EXCL_START
report("FATAL: Failed to seek to end of \"%s\": %s\n", name, strerror(errno));
return;
// LCOV_EXCL_STOP
}
}
memset(bank, padValue, sizeof(bank));
Expand All @@ -1196,8 +1212,10 @@ static void processFile(int input, int output, char const *name, off_t fileSize)
// The return value is either -1, or at most `thisLen`,
// so it's fine to cast to `size_t`
if (static_cast<size_t>(ret) != thisLen) {
// LCOV_EXCL_START
report("FATAL: Failed to write \"%s\"'s padding: %s\n", name, strerror(errno));
break;
// LCOV_EXCL_STOP
}
len -= thisLen;
}
Expand All @@ -1224,12 +1242,16 @@ static bool processFilename(char const *name) {
Defer closeInput{[&] { close(input); }};
struct stat stat;
if (fstat(input, &stat) == -1) {
// LCOV_EXCL_START
report("FATAL: Failed to stat \"%s\": %s\n", name, strerror(errno));
} else if (!S_ISREG(stat.st_mode)) { // TODO: Do we want to support other types?
// LCOV_EXCL_STOP
} else if (!S_ISREG(stat.st_mode)) { // TODO: Do we want to support FIFOs or symlinks?
// LCOV_EXCL_START
report(
"FATAL: \"%s\" is not a regular file, and thus cannot be modified in-place\n",
name
);
// LCOV_EXCL_STOP
} else if (stat.st_size < 0x150) {
// This check is in theory redundant with the one in `processFile`, but it
// prevents passing a file size of 0, which usually indicates pipes
Expand Down Expand Up @@ -1423,16 +1445,20 @@ int main(int argc, char *argv[]) {
}

case 'V':
// LCOV_EXCL_START
printf("rgbfix %s\n", get_package_version_string());
exit(0);
// LCOV_EXCL_STOP

case 'v':
fixSpec = FIX_LOGO | FIX_HEADER_SUM | FIX_GLOBAL_SUM;
break;

default:
// LCOV_EXCL_START
printUsage();
exit(1);
// LCOV_EXCL_STOP
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/gfx/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,15 @@ void fatal(char const *fmt, ...) {
}

void Options::verbosePrint(uint8_t level, char const *fmt, ...) const {
// LCOV_EXCL_START
if (verbosity >= level) {
va_list ap;

va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
// LCOV_EXCL_STOP
}

// Short options
Expand Down Expand Up @@ -586,13 +588,17 @@ static char *parseArgv(int argc, char *argv[]) {
options.tilemap = musl_optarg;
break;
case 'V':
// LCOV_EXCL_START
printf("rgbgfx %s\n", get_package_version_string());
exit(0);
// LCOV_EXCL_STOP
case 'v':
// LCOV_EXCL_START
if (options.verbosity < Options::VERB_VVVVVV) {
++options.verbosity;
}
break;
// LCOV_EXCL_STOP
case 'x':
options.trim = parseNumber(arg, "Number of tiles to trim", 0);
if (*arg != '\0') {
Expand Down Expand Up @@ -750,6 +756,7 @@ int main(int argc, char *argv[]) {
parseExternalPalSpec(localOptions.externalPalSpec);
}

// LCOV_EXCL_START
if (options.verbosity >= Options::VERB_CFG) {
fprintf(stderr, "rgbgfx %s\n", get_package_version_string());

Expand Down Expand Up @@ -862,6 +869,7 @@ int main(int argc, char *argv[]) {
printPath("Output palettes", options.palettes);
fputs("Ready.\n", stderr);
}
// LCOV_EXCL_STOP

// Do not do anything if option parsing went wrong.
requireZeroErrors();
Expand Down
4 changes: 4 additions & 0 deletions src/gfx/pal_packing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ std::tuple<DefaultInitVec<size_t>, size_t>
}
}

// LCOV_EXCL_START
if (options.verbosity >= Options::VERB_INTERM) {
for (auto &&assignment : assignments) {
fprintf(stderr, "{ ");
Expand All @@ -571,11 +572,13 @@ std::tuple<DefaultInitVec<size_t>, size_t>
fprintf(stderr, "} (volume = %zu)\n", assignment.volume());
}
}
// LCOV_EXCL_STOP

// "Decant" the result
decant(assignments, protoPalettes);
// Note that the result does not contain any empty palettes

// LCOV_EXCL_START
if (options.verbosity >= Options::VERB_INTERM) {
for (auto &&assignment : assignments) {
fprintf(stderr, "{ ");
Expand All @@ -588,6 +591,7 @@ std::tuple<DefaultInitVec<size_t>, size_t>
fprintf(stderr, "} (volume = %zu)\n", assignment.volume());
}
}
// LCOV_EXCL_STOP

DefaultInitVec<size_t> mappings(protoPalettes.size());
for (size_t i = 0; i < assignments.size(); ++i) {
Expand Down
2 changes: 1 addition & 1 deletion src/gfx/pal_sorting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void sortIndexed(
return true;
}
}
unreachable_(); // This should not be possible
unreachable_(); // LCOV_EXCL_LINE
});
}
}
Expand Down
Loading

0 comments on commit f84c2d6

Please sign in to comment.