test-tables.h revision bf502e636be8f76e05b0334b4e78ea7a398ba241
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer/***
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer This file is part of systemd
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Copyright 2013 Zbigniew Jędrzejewski-Szmek
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer systemd is free software; you can redistribute it and/or modify it
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer under the terms of the GNU Lesser General Public License as published by
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer the Free Software Foundation; either version 2.1 of the License, or
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer (at your option) any later version.
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
d9890f4ed47c0d565915360d8bae3b7a1428f285Harald Hoyer systemd is distributed in the hope that it will be useful, but
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer WITHOUT ANY WARRANTY; without even the implied warranty of
7c1856aacd4f5d41dc320860490afda18c4a7495Harald Hoyer MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7c1856aacd4f5d41dc320860490afda18c4a7495Harald Hoyer Lesser General Public License for more details.
7c1856aacd4f5d41dc320860490afda18c4a7495Harald Hoyer
7c1856aacd4f5d41dc320860490afda18c4a7495Harald Hoyer You should have received a copy of the GNU Lesser General Public License
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer along with systemd; If not, see <http://www.gnu.org/licenses/>.
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer***/
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer#include <stdio.h>
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer#include <stdlib.h>
32d965851d8cbb39f8ee0eeaf76a89e8f5fc174fLennart Poettering
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyertypedef const char* (*lookup_t)(int);
32d965851d8cbb39f8ee0eeaf76a89e8f5fc174fLennart Poetteringtypedef int (*reverse_t)(const char*);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
32d965851d8cbb39f8ee0eeaf76a89e8f5fc174fLennart Poetteringstatic inline void _test_table(const char *name,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer lookup_t lookup,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer reverse_t reverse,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int size) {
d9890f4ed47c0d565915360d8bae3b7a1428f285Harald Hoyer int i;
d9890f4ed47c0d565915360d8bae3b7a1428f285Harald Hoyer
d9890f4ed47c0d565915360d8bae3b7a1428f285Harald Hoyer for (i = 0; i < size + 1; i++) {
d9890f4ed47c0d565915360d8bae3b7a1428f285Harald Hoyer const char* val = lookup(i);
d9890f4ed47c0d565915360d8bae3b7a1428f285Harald Hoyer int rev;
d9890f4ed47c0d565915360d8bae3b7a1428f285Harald Hoyer
d9890f4ed47c0d565915360d8bae3b7a1428f285Harald Hoyer if (val)
d9890f4ed47c0d565915360d8bae3b7a1428f285Harald Hoyer rev = reverse(val);
d9890f4ed47c0d565915360d8bae3b7a1428f285Harald Hoyer else
d9890f4ed47c0d565915360d8bae3b7a1428f285Harald Hoyer rev = reverse("--no-such--value----");
d9890f4ed47c0d565915360d8bae3b7a1428f285Harald Hoyer
d9890f4ed47c0d565915360d8bae3b7a1428f285Harald Hoyer printf("%s: %d → %s → %d\n", name, i, val, rev);
d9890f4ed47c0d565915360d8bae3b7a1428f285Harald Hoyer if (i < size ? val == NULL || rev != i : val != NULL || rev != -1)
d9890f4ed47c0d565915360d8bae3b7a1428f285Harald Hoyer exit(EXIT_FAILURE);
d9890f4ed47c0d565915360d8bae3b7a1428f285Harald Hoyer }
d9890f4ed47c0d565915360d8bae3b7a1428f285Harald Hoyer}
d9890f4ed47c0d565915360d8bae3b7a1428f285Harald Hoyer
d9890f4ed47c0d565915360d8bae3b7a1428f285Harald Hoyer#define test_table(lower, upper) \
d9890f4ed47c0d565915360d8bae3b7a1428f285Harald Hoyer _test_table(STRINGIFY(lower), lower##_to_string, lower##_from_string, _##upper##_MAX)
d9890f4ed47c0d565915360d8bae3b7a1428f285Harald Hoyer