| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * roc_driver.c | |
| 3 * | |
| 4 * test driver for rollover counter replay implementation | |
| 5 * | |
| 6 * David A. McGrew | |
| 7 * Cisco Systems, Inc. | |
| 8 */ | |
| 9 | |
| 10 /* | |
| 11 * | |
| 12 * Copyright (c) 2001-2006, Cisco Systems, Inc. | |
| 13 * All rights reserved. | |
| 14 * | |
| 15 * Redistribution and use in source and binary forms, with or without | |
| 16 * modification, are permitted provided that the following conditions | |
| 17 * are met: | |
| 18 * | |
| 19 * Redistributions of source code must retain the above copyright | |
| 20 * notice, this list of conditions and the following disclaimer. | |
| 21 * | |
| 22 * Redistributions in binary form must reproduce the above | |
| 23 * copyright notice, this list of conditions and the following | |
| 24 * disclaimer in the documentation and/or other materials provided | |
| 25 * with the distribution. | |
| 26 * | |
| 27 * Neither the name of the Cisco Systems, Inc. nor the names of its | |
| 28 * contributors may be used to endorse or promote products derived | |
| 29 * from this software without specific prior written permission. | |
| 30 * | |
| 31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
| 34 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
| 35 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
| 36 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 37 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| 38 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
| 40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 41 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
| 42 * OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 43 * | |
| 44 */ | |
| 45 | |
| 46 | |
| 47 #ifdef HAVE_CONFIG_H | |
| 48 #include <config.h> | |
| 49 #endif | |
| 50 | |
| 51 #include <stdio.h> | |
| 52 | |
| 53 /* | |
| 54 * defining ROC_TEST causes small datatypes to be used in | |
| 55 * xtd_seq_num_t - this allows the functions to be exhaustively tested. | |
| 56 */ | |
| 57 #if ROC_NEEDS_TO_BE_TESTED | |
| 58 #define ROC_TEST | |
| 59 #endif | |
| 60 | |
| 61 #include "rdbx.h" | |
| 62 #include "ut_sim.h" | |
| 63 | |
| 64 err_status_t | |
| 65 roc_test(int num_trials); | |
| 66 | |
| 67 int | |
| 68 main (void) { | |
| 69 err_status_t status; | |
| 70 | |
| 71 printf("rollover counter test driver\n" | |
| 72 "David A. McGrew\n" | |
| 73 "Cisco Systems, Inc.\n"); | |
| 74 | |
| 75 printf("testing index functions..."); | |
| 76 status = roc_test(1 << 18); | |
| 77 if (status) { | |
| 78 printf("failed\n"); | |
| 79 exit(status); | |
| 80 } | |
| 81 printf("passed\n"); | |
| 82 return 0; | |
| 83 } | |
| 84 | |
| 85 | |
| 86 #define ROC_VERBOSE 0 | |
| 87 | |
| 88 err_status_t | |
| 89 roc_test(int num_trials) { | |
| 90 xtd_seq_num_t local, est, ref; | |
| 91 ut_connection utc; | |
| 92 int i, num_bad_est = 0; | |
| 93 int delta; | |
| 94 uint32_t ircvd; | |
| 95 double failure_rate; | |
| 96 | |
| 97 index_init(&local); | |
| 98 index_init(&ref); | |
| 99 index_init(&est); | |
| 100 | |
| 101 printf("\n\ttesting sequential insertion..."); | |
| 102 for (i=0; i < 2048; i++) { | |
| 103 delta = index_guess(&local, &est, (uint16_t) ref); | |
| 104 #if ROC_VERBOSE | |
| 105 printf("%lld, %lld, %d\n", ref, est, i); | |
| 106 #endif | |
| 107 if (ref != est) { | |
| 108 #if ROC_VERBOSE | |
| 109 printf(" *bad estimate*\n"); | |
| 110 #endif | |
| 111 ++num_bad_est; | |
| 112 } | |
| 113 index_advance(&ref, 1); | |
| 114 } | |
| 115 failure_rate = (double) num_bad_est / num_trials; | |
| 116 if (failure_rate > 0.01) { | |
| 117 printf("error: failure rate too high (%d bad estimates in %d trials)\n", | |
| 118 num_bad_est, num_trials); | |
| 119 return err_status_algo_fail; | |
| 120 } | |
| 121 printf("done\n"); | |
| 122 | |
| 123 | |
| 124 printf("\ttesting non-sequential insertion..."); | |
| 125 index_init(&local); | |
| 126 index_init(&ref); | |
| 127 index_init(&est); | |
| 128 ut_init(&utc); | |
| 129 | |
| 130 for (i=0; i < num_trials; i++) { | |
| 131 | |
| 132 /* get next seq num from unreliable transport simulator */ | |
| 133 ircvd = ut_next_index(&utc); | |
| 134 | |
| 135 /* set ref to value of ircvd */ | |
| 136 ref = ircvd; | |
| 137 | |
| 138 /* estimate index based on low bits of ircvd */ | |
| 139 delta = index_guess(&local, &est, (uint16_t) ref); | |
| 140 #if ROC_VERBOSE | |
| 141 printf("ref: %lld, local: %lld, est: %lld, ircvd: %d, delta: %d\n", | |
| 142 ref, local, est, ircvd, delta); | |
| 143 #endif | |
| 144 | |
| 145 if (local + delta != est) { | |
| 146 printf(" *bad delta*: local %llu + delta %d != est %llu\n", | |
| 147 (unsigned long long)local, delta, (unsigned long long)est); | |
| 148 return err_status_algo_fail; | |
| 149 } | |
| 150 | |
| 151 /* now update local xtd_seq_num_t as necessary */ | |
| 152 if (delta > 0) | |
| 153 index_advance(&local, delta); | |
| 154 | |
| 155 if (ref != est) { | |
| 156 #if ROC_VERBOSE | |
| 157 printf(" *bad estimate*\n"); | |
| 158 #endif | |
| 159 /* record failure event */ | |
| 160 ++num_bad_est; | |
| 161 | |
| 162 /* reset local value to correct value */ | |
| 163 local = ref; | |
| 164 } | |
| 165 } | |
| 166 failure_rate = (double) num_bad_est / num_trials; | |
| 167 if (failure_rate > 0.01) { | |
| 168 printf("error: failure rate too high (%d bad estimates in %d trials)\n", | |
| 169 num_bad_est, num_trials); | |
| 170 return err_status_algo_fail; | |
| 171 } | |
| 172 printf("done\n"); | |
| 173 | |
| 174 return err_status_ok; | |
| 175 } | |
| OLD | NEW |