E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 1 | // : vi ts=4 sw=4 noet : |
| 2 | /* |
| 3 | ================================================================================== |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 4 | Copyright (c) 2019 Nokia |
| 5 | Copyright (c) 2018-2019 AT&T Intellectual Property. |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 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 | |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 11 | http://www.apache.org/licenses/LICENSE-2.0 |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 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 |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 24 | time by the test driver. |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 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> |
E. Scott Daniels | 412d53d | 2019-05-20 20:00:52 +0000 | [diff] [blame] | 37 | #include <pthread.h> |
| 38 | #include <semaphore.h> |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 39 | |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 40 | /* |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 41 | Conduct a series of interleaved tests inserting i-factor |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 42 | values before beginning to pull values (i-factor must be |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 43 | size - 2 smaller than the ring. |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 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; |
E. Scott Daniels | c1f84f8 | 2020-01-22 12:31:07 -0500 | [diff] [blame] | 77 | int pfd = -1; // pollable file descriptor for the ring |
| 78 | int errors = 0; |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 79 | |
| 80 | r = uta_mk_ring( 0 ); // should return nil |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame] | 81 | errors += fail_not_nil( r, "attempt to make a ring with size 0 returned a pointer" ); |
| 82 | |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 83 | r = uta_mk_ring( -1 ); // should also return nil |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame] | 84 | errors += fail_not_nil( r, "attempt to make a ring with negative size returned a pointer" ); |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 85 | |
| 86 | r = uta_mk_ring( 18 ); |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame] | 87 | errors += fail_if_nil( r, "attempt to make a ring with valid size returned a nil pointer" ); |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 88 | |
E. Scott Daniels | c1f84f8 | 2020-01-22 12:31:07 -0500 | [diff] [blame] | 89 | pfd = uta_ring_getpfd( r ); // get pollable file descriptor |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame] | 90 | errors += fail_if_true( pfd < 0, "pollable file descriptor returned was bad" ); |
E. Scott Daniels | c1f84f8 | 2020-01-22 12:31:07 -0500 | [diff] [blame] | 91 | |
| 92 | pfd = uta_ring_config( r, 0x03 ); // turn on locking for reads and writes |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame] | 93 | errors += fail_if_true( pfd != 1, "attempt to enable locking failed" ); |
E. Scott Daniels | c1f84f8 | 2020-01-22 12:31:07 -0500 | [diff] [blame] | 94 | |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 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 | |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame] | 102 | errors += fail_if_true( i > size, "ring insert did not report full table" ); |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 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 ) { |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame] | 106 | errors += fail_if_true( i < size-1, "nil pointer on extract from full table" ); |
| 107 | break; |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 108 | } |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 109 | |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame] | 110 | if( fail_if_true( *dp != i, "extracted data is incorrect; see details below" )) { |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 111 | fprintf( stderr, "<FAIL> data at i=% isnt right; expected %d got %d\n", i, i, *dp ); |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame] | 112 | errors++; |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 113 | } |
| 114 | } |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame] | 115 | fail_if_true( i > size, "got too many values from extract loop" ); |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 116 | |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 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 ); |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame] | 121 | errors += fail_not_equal( ie_test( r, i, 101 ), 0, "ie test for 101 inserts didn't return 0" ); |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 122 | |
| 123 | uta_ring_free( r ); |
| 124 | } |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 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 ); |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame] | 130 | errors += fail_not_equal( ie_test( r, i, 66000 ), 0, "ie test for 66K inserts didn't return 0" ); |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 131 | |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 132 | uta_ring_free( r ); |
| 133 | } |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 134 | |
| 135 | size++; |
| 136 | } |
| 137 | |
E. Scott Daniels | c1f84f8 | 2020-01-22 12:31:07 -0500 | [diff] [blame] | 138 | return errors; |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 139 | } |