test_zone_serial.c 3.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*  Copyright (C) 2015 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <tap/basic.h>
#include <stdlib.h>

#include "knot/zone/serial.h"

enum serials {
	S_LOWEST = 0,			// lowest value
	S_2LOWEST = 1,			// second lowest value
	S_BELOW_MIDDLE = 0x7fffffff,	// one below middle
	S_ABOVE_MIDDLE = 0x80000000,	// one above middle
	S_2HIGHEST = 0xffffffff - 1,	// second highest value
	S_HIGHEST = 0xffffffff		// highest value
};

31
static uint32_t random_serial(void)
32 33 34 35 36 37 38 39 40 41 42 43 44 45
{
	uint32_t s = rand() & 0xff;
	s |= (rand() & 0xff) << 8;
	s |= (rand() & 0xff) << 16;
	s |= (rand() & 0xff) << 24;

	return s;
}

int main(int argc, char *argv[])
{
	plan(20);

	/* Serial compare test. */
Libor Peltan's avatar
Libor Peltan committed
46
	ok(serial_compare(S_LOWEST, S_BELOW_MIDDLE) == SERIAL_LOWER,
47
	   "serial compare: lowest < below middle");
Libor Peltan's avatar
Libor Peltan committed
48
	ok(serial_compare(S_BELOW_MIDDLE, S_LOWEST) == SERIAL_GREATER,
49 50 51
	   "serial compare: below middle > lowest");

	/* Corner-case: these serials' distance is exactly 2^31. */
Libor Peltan's avatar
Libor Peltan committed
52
	ok(serial_compare(S_LOWEST, S_ABOVE_MIDDLE) == SERIAL_INCOMPARABLE,
53
	   "serial compare: lowest < above_middle");
Libor Peltan's avatar
Libor Peltan committed
54
	ok(serial_compare(S_ABOVE_MIDDLE, S_LOWEST) == SERIAL_INCOMPARABLE,
55 56
	   "serial compare: above_middle < lowest");

Libor Peltan's avatar
Libor Peltan committed
57
	ok(serial_compare(S_LOWEST, S_HIGHEST) == SERIAL_GREATER,
58
	   "serial compare: lowest > highest");
Libor Peltan's avatar
Libor Peltan committed
59
	ok(serial_compare(S_HIGHEST, S_LOWEST) == SERIAL_LOWER,
60 61
	   "serial compare: highest < lowest");

Libor Peltan's avatar
Libor Peltan committed
62
	ok(serial_compare(S_2LOWEST, S_ABOVE_MIDDLE) == SERIAL_LOWER,
63
	   "serial compare: 2nd lowest < above middle");
Libor Peltan's avatar
Libor Peltan committed
64
	ok(serial_compare(S_ABOVE_MIDDLE, S_2LOWEST) == SERIAL_GREATER,
65 66 67
	   "serial compare: above middle > 2nd lowest");

	/* Corner-case: these serials' distance is exactly 2^31. */
Libor Peltan's avatar
Libor Peltan committed
68
	ok(serial_compare(S_BELOW_MIDDLE, S_HIGHEST) == SERIAL_INCOMPARABLE,
69
	   "serial compare: below middle < highest");
Libor Peltan's avatar
Libor Peltan committed
70
	ok(serial_compare(S_HIGHEST, S_BELOW_MIDDLE) == SERIAL_INCOMPARABLE,
71 72
	   "serial compare: highest < below middle");

Libor Peltan's avatar
Libor Peltan committed
73
	ok(serial_compare(S_BELOW_MIDDLE, S_2HIGHEST) == SERIAL_LOWER,
74
	   "serial compare: below middle < 2nd highest");
Libor Peltan's avatar
Libor Peltan committed
75
	ok(serial_compare(S_2HIGHEST, S_BELOW_MIDDLE) == SERIAL_GREATER,
76 77
	   "serial compare: 2nd highest > below middle");

Libor Peltan's avatar
Libor Peltan committed
78
	ok(serial_compare(S_ABOVE_MIDDLE, S_HIGHEST) == SERIAL_LOWER,
79
	   "serial compare: above middle < highest");
Libor Peltan's avatar
Libor Peltan committed
80
	ok(serial_compare(S_HIGHEST, S_ABOVE_MIDDLE) == SERIAL_GREATER,
81 82
	   "serial compare: highest > above middle");

Libor Peltan's avatar
Libor Peltan committed
83
	ok(serial_compare(S_LOWEST, S_LOWEST) == SERIAL_EQUAL,
84
	   "serial compare: lowest == lowest");
Libor Peltan's avatar
Libor Peltan committed
85
	ok(serial_compare(S_HIGHEST, S_HIGHEST) == SERIAL_EQUAL,
86 87
	   "serial compare: highest == highest");

Libor Peltan's avatar
Libor Peltan committed
88
	ok(serial_compare(S_LOWEST - 1, S_HIGHEST) == SERIAL_EQUAL,
89
	   "serial compare: lowest - 1 == highest");
Libor Peltan's avatar
Libor Peltan committed
90
	ok(serial_compare(S_LOWEST, S_HIGHEST + 1) == SERIAL_EQUAL,
91 92 93 94
	   "serial compare: lowest== highest + 1");

	/* Corner-case: these serials' distance is exactly 2^31. */
	uint32_t s1 = random_serial();
95
	uint32_t s2 = s1 + S_ABOVE_MIDDLE;  // exactly the 'opposite' number
Libor Peltan's avatar
Libor Peltan committed
96
	ok(serial_compare(s1, s2) == SERIAL_INCOMPARABLE,
97
	   "serial compare: random opposites (s1 < s2)");
Libor Peltan's avatar
Libor Peltan committed
98
	ok(serial_compare(s2, s1) == SERIAL_INCOMPARABLE,
99
	   "serial compare: random opposites (s2 < s1)");
100 101 102

	return 0;
}