/* SPDX-License-Identifier: GPL-2.0-only */

#include <ppm/error.h>

#include <stdio.h>
#include <string.h>

#define CHECK(expr) do {                                            \
    if (!(expr)) {                                                  \
        fprintf(stderr, "FAIL: %s:%d: %s\n", __FILE__, __LINE__, #expr); \
        return 1;                                                   \
    }                                                               \
} while (0)

int main(void) {
    /* Every named code returns a non-empty, non-"unknown" description. */
    const ppm_error_t codes[] = {
        PPM_OK, PPM_ERR_NOMEM, PPM_ERR_IO, PPM_ERR_PARSE, PPM_ERR_INVALID,
        PPM_ERR_NOT_FOUND, PPM_ERR_EXISTS, PPM_ERR_PERMISSION, PPM_ERR_CONFLICT,
        PPM_ERR_UNSAT, PPM_ERR_HASH_MISMATCH, PPM_ERR_SIG_INVALID,
        PPM_ERR_NETWORK, PPM_ERR_REGISTRY, PPM_ERR_TXN_ABORT,
        PPM_ERR_UNSUPPORTED, PPM_ERR_INTERNAL,
    };
    for (size_t i = 0; i < sizeof(codes)/sizeof(codes[0]); i++) {
        const char *s = ppm_strerror(codes[i]);
        CHECK(s != NULL);
        CHECK(*s != '\0');
        CHECK(strcmp(s, "unknown error") != 0);
    }

    /* Out-of-range falls through to the unknown sentinel. */
    CHECK(strcmp(ppm_strerror((ppm_error_t)9999), "unknown error") == 0);

    printf("ok\n");
    return 0;
}
