|
0
|
1 //================================================================= |
|
|
2 // |
|
|
3 // bsearch.c |
|
|
4 // |
|
|
5 // Testcase for C library bsearch() |
|
|
6 // |
|
|
7 //================================================================= |
|
|
8 //####COPYRIGHTBEGIN#### |
|
|
9 // |
|
|
10 // ------------------------------------------- |
|
|
11 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
12 // Version 1.0 (the "License"); you may not use this file except in |
|
|
13 // compliance with the License. You may obtain a copy of the License at |
|
|
14 // http://sourceware.cygnus.com/ecos |
|
|
15 // |
|
|
16 // Software distributed under the License is distributed on an "AS IS" |
|
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
|
18 // License for the specific language governing rights and limitations under |
|
|
19 // the License. |
|
|
20 // |
|
|
21 // The Original Code is eCos - Embedded Cygnus Operating System, released |
|
|
22 // September 30, 1998. |
|
|
23 // |
|
|
24 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
|
25 // by Cygnus are Copyright (C) 1998 Cygnus Solutions. All Rights Reserved. |
|
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //================================================================= |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
|
32 // Author(s): ctarpy@cygnus.co.uk, jlarmour@cygnus.co.uk |
|
|
33 // Contributors: jlarmour@cygnus.co.uk |
|
|
34 // Date: 1998/6/3 |
|
|
35 // Description: Contains testcode for C library bsearch() function |
|
|
36 // |
|
|
37 // |
|
|
38 //####DESCRIPTIONEND#### |
|
|
39 |
|
|
40 // Declarations for test system: |
|
|
41 // |
|
|
42 // TESTCASE_TYPE=CYG_TEST_MODULE |
|
|
43 // COMPOUND_TESTCASE |
|
|
44 |
|
|
45 // CONFIGURATION |
|
|
46 |
|
|
47 #include <pkgconf/libc.h> // Configuration header |
|
|
48 |
|
|
49 // INCLUDES |
|
|
50 |
|
|
51 #include <stdlib.h> |
|
|
52 #include <cyg/infra/testcase.h> |
|
|
53 #include <sys/cstartup.h> // C library initialisation |
|
|
54 |
|
|
55 |
|
|
56 // FUNCTIONS |
|
|
57 |
|
|
58 externC void |
|
|
59 cyg_package_start( void ) |
|
|
60 { |
|
|
61 #ifdef CYGPKG_LIBC |
|
|
62 cyg_iso_c_start(); |
|
|
63 #else |
|
|
64 (void)main(0, NULL); |
|
|
65 #endif |
|
|
66 } // cyg_package_start() |
|
|
67 |
|
|
68 |
|
|
69 #ifdef CYGPKG_LIBC |
|
|
70 static int |
|
|
71 Compar( const void *int1, const void *int2 ) |
|
|
72 { |
|
|
73 if ( *(int*)int1 < *(int*)int2 ) |
|
|
74 return -1; |
|
|
75 else if ( *(int*)int1 == *(int*)int2 ) |
|
|
76 return 0; |
|
|
77 else |
|
|
78 return 1; |
|
|
79 } // Compar() |
|
|
80 #endif |
|
|
81 |
|
|
82 int |
|
|
83 main( int argc, char *argv[] ) |
|
|
84 { |
|
|
85 #ifdef CYGPKG_LIBC |
|
|
86 int key; |
|
|
87 int *result; |
|
|
88 int i_array[] = {1, 5, 8, 35, 84, 258, 1022, 1022, 5300, 7372, 9029}; |
|
|
89 #endif |
|
|
90 |
|
|
91 CYG_TEST_INIT(); |
|
|
92 |
|
|
93 CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library " |
|
|
94 "bsearch() function"); |
|
|
95 |
|
|
96 #ifdef CYGPKG_LIBC |
|
|
97 // Test 1 |
|
|
98 key = 8; |
|
|
99 result = bsearch(&key, i_array, sizeof(i_array)/sizeof(i_array[0]), |
|
|
100 sizeof(i_array[0]), &Compar); |
|
|
101 CYG_TEST_PASS_FAIL( (result!=NULL) && (*result == 8), |
|
|
102 "bsearch() something in the middle"); |
|
|
103 |
|
|
104 // Test 2 |
|
|
105 key = 5300; |
|
|
106 result = bsearch(&key, i_array, sizeof(i_array)/sizeof(i_array[0]), |
|
|
107 sizeof(i_array[0]), &Compar); |
|
|
108 CYG_TEST_PASS_FAIL( (result!=NULL) && (*result == 5300), |
|
|
109 "bsearch() something else in the middle"); |
|
|
110 |
|
|
111 // Test 3 |
|
|
112 key = 1; |
|
|
113 result = bsearch(&key, i_array, sizeof(i_array)/sizeof(i_array[0]), |
|
|
114 sizeof(i_array[0]), &Compar); |
|
|
115 CYG_TEST_PASS_FAIL( (result!=NULL) && (*result == 1), |
|
|
116 "bsearch() first element"); |
|
|
117 |
|
|
118 // Test 4 |
|
|
119 key = 9029; |
|
|
120 result = bsearch(&key, i_array, sizeof(i_array)/sizeof(i_array[0]), |
|
|
121 sizeof(i_array[0]), &Compar); |
|
|
122 CYG_TEST_PASS_FAIL( (result!=NULL) && (*result == 9029), |
|
|
123 "bsearch() last element"); |
|
|
124 |
|
|
125 // Test 5 |
|
|
126 key = 1022; |
|
|
127 result = bsearch(&key, i_array, sizeof(i_array)/sizeof(i_array[0]), |
|
|
128 sizeof(i_array[0]), &Compar); |
|
|
129 CYG_TEST_PASS_FAIL( (result!=NULL) && (*result == 1022), |
|
|
130 "bsearch() duplicate element"); |
|
|
131 |
|
|
132 // Test 6 |
|
|
133 key = 2; |
|
|
134 result = bsearch(&key, i_array, sizeof(i_array)/sizeof(i_array[0]), |
|
|
135 sizeof(i_array[0]), &Compar); |
|
|
136 CYG_TEST_PASS_FAIL( result==NULL, "bsearch() nonexistent element"); |
|
|
137 |
|
|
138 #else // ifndef CYGPKG_LIBC |
|
|
139 CYG_TEST_PASS("Testing is not applicable to this configuration"); |
|
|
140 #endif // ifndef CYGPKG_LIBC |
|
|
141 |
|
|
142 CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library " |
|
|
143 "bsearch() function"); |
|
|
144 |
|
|
145 } // main() |
|
|
146 |
|
|
147 |
|
|
148 // EOF bsearch.c |