1/* Test of uN_cmp() functions.
2   Copyright (C) 2008-2010 Free Software Foundation, Inc.
3
4   This program is free software: you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 3 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17/* Written by Simon Josefsson and Bruno Haible <bruno@clisp.org>, 2010.  */
18
19static void
20test_cmp (void)
21{
22  /* Test equal / not equal distinction.  */
23  ASSERT (U_CMP (zerosize_ptr (), zerosize_ptr (), 0) == 0);
24  {
25    static const UNIT input1[] = { 'f', 'o', 'o', 0 };
26    static const UNIT input2[] = { 'f', 'o', 'o', 'b', 'a', 'r', 0 };
27    ASSERT (U_CMP (input1, input2, 2) == 0);
28    ASSERT (U_CMP (input1, input2, 3) == 0);
29    ASSERT (U_CMP (input1, input2, 4) != 0);
30  }
31  {
32    static const UNIT input1[] = { 'f', 'o', 'o', 0 };
33    static const UNIT input2[] = { 'b', 'a', 'r', 0 };
34    ASSERT (U_CMP (input1, input2, 1) != 0);
35    ASSERT (U_CMP (input1, input2, 3) != 0);
36  }
37
38  /* Test less / equal / greater distinction.  */
39  {
40    static const UNIT input1[] = { 'f', 'o', 'o', 0 };
41    static const UNIT input2[] = { 'm', 'o', 'o', 0 };
42    ASSERT (U_CMP (input1, input2, 4) < 0);
43    ASSERT (U_CMP (input2, input1, 4) > 0);
44  }
45  {
46    static const UNIT input1[] = { 'o', 'o', 'm', 'p', 'h', 0 };
47    static const UNIT input2[] = { 'o', 'o', 'p', 's', 0 };
48    ASSERT (U_CMP (input1, input2, 3) < 0);
49    ASSERT (U_CMP (input2, input1, 3) > 0);
50  }
51  {
52    static const UNIT input1[] = { 'f', 'o', 'o', 0 };
53    static const UNIT input2[] = { 'f', 'o', 'o', 'b', 'a', 'r', 0 };
54    ASSERT (U_CMP (input1, input2, 4) < 0);
55    ASSERT (U_CMP (input2, input1, 4) > 0);
56  }
57
58  /* Some old versions of memcmp were not 8-bit clean.  */
59  {
60    static const UNIT input1[] = { 0x40 };
61    static const UNIT input2[] = { 0xC2 };
62    ASSERT (U_CMP (input1, input2, 1) < 0);
63    ASSERT (U_CMP (input2, input1, 1) > 0);
64  }
65  {
66    static const UNIT input1[] = { 0xC2 };
67    static const UNIT input2[] = { 0xC3 };
68    ASSERT (U_CMP (input1, input2, 1) < 0);
69    ASSERT (U_CMP (input2, input1, 1) > 0);
70  }
71
72  /* The Next x86 OpenStep bug shows up only when comparing 16 bytes
73     or more and with at least one buffer not starting on a 4-byte boundary.
74     William Lewis provided this test program.   */
75  {
76    UNIT foo[21];
77    UNIT bar[21];
78    int i;
79    for (i = 0; i < 4; i++)
80      {
81        UNIT *a = foo + i;
82        UNIT *b = bar + i;
83        int j;
84        for (j = 0; j < 8; j++)
85          a[j] = '-';
86        a[8] = '0';
87        for (j = 9; j < 16; j++)
88          a[j] = '1';
89        for (j = 0; j < 8; j++)
90          b[j] = '-';
91        b[8] = '1';
92        for (j = 9; j < 16; j++)
93          b[j] = '0';
94        ASSERT (U_CMP (a, b, 16) < 0);
95      }
96  }
97}
98