CI: Add silent cmake SonarCloud scan
[ric-plt/lib/rmr.git] / test / ring_static_test.c
1 // : vi ts=4 sw=4 noet :
2 /*
3 ==================================================================================
4             Copyright (c) 2019 Nokia
5             Copyright (c) 2018-2019 AT&T Intellectual Property.
6
7    Licensed under the Apache License, Version 2.0 (the "License");
8    you may not use this file except in compliance with the License.
9    You may obtain a copy of the License at
10
11            http://www.apache.org/licenses/LICENSE-2.0
12
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18 ==================================================================================
19 */
20
21 /*
22         Mmemonic:       ring_static_test.c
23         Abstract:       Test the ring funcitons. These are meant to be included at compile
24                                 time by the test driver.
25
26         Author:         E. Scott Daniels
27         Date:           3 April 2019
28 */
29
30 #include <unistd.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <strings.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <stdint.h>
37 #include <pthread.h>
38 #include <semaphore.h>
39
40 /*
41         Conduct a series of interleaved tests inserting i-factor
42         values before beginning to pull values (i-factor must be
43         size - 2 smaller than the ring.
44         Returns 0 on success, 1 on insert failure and 2 on pull failure.
45 */
46 static int ie_test( void* r, int i_factor, long inserts ) {
47         int i;
48         int* dp;
49         int data[29];
50
51         for( i = 0; i < inserts; i++ ) {
52                 data[i%29] = i;
53                 if( ! uta_ring_insert( r, &data[i%29] ) ) {
54                         fprintf( stderr, "<FAIL> interleaved insert failed on ifactor=%d i=%d\n", i_factor, i );
55                         return 1;
56                 }
57                 if( i > i_factor-1 ) {
58                         dp = uta_ring_extract( r );
59                         if( *dp != data[(i-i_factor)%29] ) {
60                                 fprintf( stderr, "<FAIL> interleaved exctract failed on ifactor=%d i=%d expected=%d got=%d\n", i_factor, i, data[(i-i_factor)%29], *dp );
61                                 return 2;
62                         }
63                 }
64         }
65         //fprintf( stderr, "<OK>   interleaved insert/extract test passed for insert factor %d\n", i_factor );
66
67         return 0;
68 }
69
70 static int ring_test( ) {
71         void* r;
72         int i;
73         int j;
74         int     data[20];
75         int*    dp;
76         int size = 18;
77         int     pfd = -1;                                       // pollable file descriptor for the ring
78         int     errors = 0;
79
80         r = uta_mk_ring( 0 );                   // should return nil
81         errors += fail_not_nil( r, "attempt to make a ring with size 0 returned a pointer" );
82
83         r = uta_mk_ring( -1 );                  // should also return nil
84         errors += fail_not_nil( r, "attempt to make a ring with negative size returned a pointer" );
85
86         r = uta_mk_ring( 18 );
87         errors += fail_if_nil( r, "attempt to make a ring with valid size returned a nil pointer" );
88
89         pfd = uta_ring_getpfd( r );             // get pollable file descriptor
90         errors += fail_if_true( pfd < 0, "pollable file descriptor returned was bad" );
91
92         pfd = uta_ring_config( r, 0x03 );               // turn on locking for reads and writes
93         errors += fail_if_true( pfd != 1, "attempt to enable locking failed" );
94
95         for( i = 0; i < 20; i++ ) {             // test to ensure it reports full when head/tail start at 0
96                 data[i] = i;
97                 if( ! uta_ring_insert( r, &data[i] ) ) {
98                         break;
99                 }
100         }
101
102         errors += fail_if_true( i > size, "ring insert did not report full table" );
103
104         for( i = 0; i < size + 3; i++ ) {                                                               // ensure they all come back in order, and we don't get 'extras'
105                 if( (dp = uta_ring_extract( r )) == NULL ) {
106                         errors += fail_if_true( i < size-1, "nil pointer on extract from full table" );
107                         break;
108                 }
109
110                 if( fail_if_true( *dp != i, "extracted data is incorrect; see details below" )) {
111                         fprintf( stderr, "<FAIL> data at i=% isnt right; expected %d got %d\n", i, i, *dp );
112                         errors++;
113                 }
114         }
115         fail_if_true( i > size, "got too many values from extract loop" );
116
117         uta_ring_free( NULL );                                                  // ensure this doesn't blow up
118         uta_ring_free( r );
119         for( i = 2; i < 15; i++ ) {
120                 r = uta_mk_ring( 16 );
121                 errors += fail_not_equal( ie_test( r, i, 101 ), 0, "ie test for 101 inserts didn't return 0" );
122
123                 uta_ring_free( r );
124         }
125
126         size = 5;
127         for( j = 0; j < 20; j++ ) {
128                 for( i = 2; i < size - 2; i++ ) {
129                         r = uta_mk_ring( size );
130                         errors += fail_not_equal( ie_test( r, i, 66000 ), 0, "ie test for 66K inserts didn't return 0" );
131
132                         uta_ring_free( r );
133                 }
134
135                 size++;
136         }
137
138         return errors;
139 }