1/* Test of uN_chr() 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 Eric Blake and Bruno Haible <bruno@clisp.org>, 2010. */ 18 19int 20main (void) 21{ 22 size_t n = 0x100000; 23 UNIT *input = (UNIT *) malloc (n * sizeof (UNIT)); 24 ASSERT (input); 25 26 input[0] = 'a'; 27 input[1] = 'b'; 28 U_SET (input + 2, 'c', 1024); 29 U_SET (input + 1026, 'd', n - 1028); 30 input[n - 2] = 'e'; 31 input[n - 1] = 'a'; 32 33 /* Basic behavior tests. */ 34 ASSERT (U_CHR (input, n, 'a') == input); 35 36 ASSERT (U_CHR (input, 0, 'a') == NULL); 37 ASSERT (U_CHR (zerosize_ptr (), 0, 'a') == NULL); 38 39 ASSERT (U_CHR (input, n, 'b') == input + 1); 40 ASSERT (U_CHR (input, n, 'c') == input + 2); 41 ASSERT (U_CHR (input, n, 'd') == input + 1026); 42 43 ASSERT (U_CHR (input + 1, n - 1, 'a') == input + n - 1); 44 ASSERT (U_CHR (input + 1, n - 1, 'e') == input + n - 2); 45 46 ASSERT (U_CHR (input, n, 'f') == NULL); 47 ASSERT (U_CHR (input, n, '\0') == NULL); 48 49 /* Check that a very long haystack is handled quickly if the byte is 50 found near the beginning. */ 51 { 52 size_t repeat = 10000; 53 for (; repeat > 0; repeat--) 54 { 55 ASSERT (U_CHR (input, n, 'c') == input + 2); 56 } 57 } 58 59 /* Alignment tests. */ 60 { 61 int i, j; 62 for (i = 0; i < 32; i++) 63 { 64 for (j = 0; j < 128; j++) 65 input[i + j] = j; 66 for (j = 0; j < 128; j++) 67 { 68 ASSERT (U_CHR (input + i, 128, j) == input + i + j); 69 } 70 } 71 } 72 73 /* Check that uN_chr() does not read past the first occurrence of the 74 byte being searched. */ 75 { 76 char *page_boundary = (char *) zerosize_ptr (); 77 78 if (page_boundary != NULL) 79 { 80 for (n = 1; n <= 500 / sizeof (UNIT); n++) 81 { 82 UNIT *mem = (UNIT *) (page_boundary - n * sizeof (UNIT)); 83 U_SET (mem, 'X', n); 84 ASSERT (U_CHR (mem, n, 'U') == NULL); 85 86 { 87 size_t i; 88 89 for (i = 0; i < n; i++) 90 { 91 mem[i] = 'U'; 92 ASSERT (U_CHR (mem, 4000, 'U') == mem + i); 93 mem[i] = 'X'; 94 } 95 } 96 } 97 } 98 } 99 100 free (input); 101 102 return 0; 103} 104