t_relative_path.c (1453B)
- // utils-std: Collection of commonly available Unix tools
- // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
- // SPDX-License-Identifier: MPL-2.0
- #define _POSIX_C_SOURCE 200809L
- #include "../lib/relative_path.h"
- #include <assert.h>
- #include <stdio.h>
- #include <string.h>
- #define TEST_PLAN 7
- int counter = 0;
- static int
- t_relpath(const char *base, const char *target, const char *expected)
- {
- int id = ++counter;
- static char buf[PATH_MAX];
- if(relative_path(base, target, buf) < 0)
- {
- fprintf(stderr, "# returned -1\n");
- fprintf(stderr, "not ok %d - ('%s', '%s') == '%s'\n", id, base, target, expected);
- return 1;
- }
- if(strcmp(buf, expected) != 0)
- {
- fprintf(stderr, "# Got '%s'\n", buf);
- fprintf(stderr, "not ok %d - ('%s', '%s') == '%s'\n", id, base, target, expected);
- return 1;
- }
- fprintf(stderr, "ok %d - ('%s', '%s') == '%s'\n", id, base, target, expected);
- return 0;
- }
- int
- main(void)
- {
- printf("1..%d\n", TEST_PLAN);
- int err = 0;
- err += t_relpath("/usr/bin/a", "/usr/bin/b", "b");
- err += t_relpath("/usr/a", "/usr/bin/b", "bin/b");
- err += t_relpath("/usr/bin/a", "/usr/b", "../b");
- err += t_relpath("/usr/bin/a", "/usr/lib/b", "../lib/b");
- err += t_relpath("/", "/", ".");
- err += t_relpath("/a", "/", ".");
- err += t_relpath("/", "/b", "b");
- if(counter != TEST_PLAN)
- {
- fprintf(stderr, "# planned for %d tests, ran %d\n", TEST_PLAN, counter);
- return 1;
- }
- return err;
- }