// SPDX-License-Identifier: GPL-2.0-only
/*
 *  ppm/tests/test_version_req.c
 *
 *  Copyright (C) 2026 Project Tick
 */

#include "core/version_req.h"

#include <stdio.h>

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

static ppm_version_t V(unsigned a, unsigned b, unsigned c) {
    return (ppm_version_t){ .major = a, .minor = b, .patch = c };
}

int main(void) {
    ppm_version_t v   = V(1,2,3);
    ppm_version_t v199 = V(1,9,9);
    ppm_version_t v200 = V(2,0,0);
    ppm_version_t v122 = V(1,2,2);
    ppm_version_t v027 = V(0,2,7);
    ppm_version_t v030 = V(0,3,0);
    ppm_version_t v129 = V(1,2,9);
    ppm_version_t v130 = V(1,3,0);
    ppm_version_t v120 = V(1,2,0);

    /* Exact / equality. */
    CHECK(ppm_version_req_match("1.2.3", &v));
    CHECK(ppm_version_req_match("=1.2.3", &v));
    CHECK(!ppm_version_req_match("1.2.4", &v));

    /* Caret. */
    CHECK(ppm_version_req_match("^1.2.3", &v));
    CHECK(ppm_version_req_match("^1.2.3", &v199));
    CHECK(!ppm_version_req_match("^1.2.3", &v200));
    CHECK(!ppm_version_req_match("^1.2.3", &v122));
    CHECK(ppm_version_req_match("^0.2.0",  &v027));
    CHECK(!ppm_version_req_match("^0.2.0", &v030));

    /* Tilde. */
    CHECK(ppm_version_req_match("~1.2.3",  &v129));
    CHECK(!ppm_version_req_match("~1.2.3", &v130));

    /* Comparators. */
    CHECK(ppm_version_req_match(">=1.0.0", &v));
    CHECK(!ppm_version_req_match(">1.2.3", &v));
    CHECK(ppm_version_req_match("<2.0.0", &v));
    CHECK(ppm_version_req_match("<=1.2.3", &v));

    /* Conjunction. */
    CHECK(ppm_version_req_match(">=1.0.0, <2.0.0",  &v));
    CHECK(!ppm_version_req_match(">=1.0.0, <1.2.0", &v));

    /* Wildcard / empty. */
    CHECK(ppm_version_req_match("*", &v));
    CHECK(ppm_version_req_match("",  &v));

    /* Two-component "1.2" treated as 1.2.0 — bare match. */
    CHECK(ppm_version_req_match("1.2", &v120));

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