| 1 |
/* |
| 2 |
* Copyright (C) 2017 Jolla Ltd. |
| 3 |
* Contact: Slava Monich <[email protected]> |
| 4 |
* |
| 5 |
* You may use this file under the terms of BSD license as follows: |
| 6 |
* |
| 7 |
* Redistribution and use in source and binary forms, with or without |
| 8 |
* modification, are permitted provided that the following conditions |
| 9 |
* are met: |
| 10 |
* |
| 11 |
* 1. Redistributions of source code must retain the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer. |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* 3. Neither the name of Jolla Ltd nor the names of its contributors may |
| 17 |
* be used to endorse or promote products derived from this software |
| 18 |
* without specific prior written permission. |
| 19 |
* |
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 21 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS |
| 24 |
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 27 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 29 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 30 |
* THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
*/ |
| 32 |
|
| 33 |
#include "test_common.h" |
| 34 |
|
| 35 |
#include "gutil_history.h" |
| 36 |
|
| 37 |
static TestOpt test_opt; |
| 38 |
static gint64 test_history_time; |
| 39 |
|
| 40 |
static |
| 41 |
gint64 |
| 42 |
test_history_time_func(void) |
| 43 |
{ |
| 44 |
return test_history_time; |
| 45 |
} |
| 46 |
|
| 47 |
/*==========================================================================* |
| 48 |
* NULL tolerance |
| 49 |
*==========================================================================*/ |
| 50 |
|
| 51 |
static |
| 52 |
void |
| 53 |
test_history_null( |
| 54 |
void) |
| 55 |
{ |
| 56 |
gutil_int_history_unref(NULL); |
| 57 |
gutil_int_history_clear(NULL); |
| 58 |
g_assert(!gutil_int_history_ref(NULL)); |
| 59 |
g_assert(!gutil_int_history_size(NULL)); |
| 60 |
g_assert(!gutil_int_history_interval(NULL)); |
| 61 |
g_assert(!gutil_int_history_add(NULL, 1)); |
| 62 |
g_assert(!gutil_int_history_median(NULL, 0)); |
| 63 |
g_assert(!gutil_int_history_new(0, 0)); |
| 64 |
g_assert(!gutil_int_history_new(1, 0)); |
| 65 |
g_assert(!gutil_int_history_new(0, 1)); |
| 66 |
} |
| 67 |
|
| 68 |
/*==========================================================================* |
| 69 |
* Basic |
| 70 |
*==========================================================================*/ |
| 71 |
|
| 72 |
static |
| 73 |
void |
| 74 |
test_history_basic( |
| 75 |
void) |
| 76 |
{ |
| 77 |
/* The default time functon returns real time, not usable for testing */ |
| 78 |
GUtilIntHistory* h = gutil_int_history_new(1, 1); |
| 79 |
g_assert(gutil_int_history_size(h) == 0); |
| 80 |
g_assert(gutil_int_history_interval(h) == 0); |
| 81 |
gutil_int_history_unref(gutil_int_history_ref(h)); |
| 82 |
gutil_int_history_unref(h); |
| 83 |
} |
| 84 |
|
| 85 |
/*==========================================================================* |
| 86 |
* Clear |
| 87 |
*==========================================================================*/ |
| 88 |
|
| 89 |
static |
| 90 |
void |
| 91 |
test_history_clear( |
| 92 |
void) |
| 93 |
{ |
| 94 |
GUtilIntHistory* h = gutil_int_history_new_full(2, 2, |
| 95 |
test_history_time_func); |
| 96 |
g_assert(gutil_int_history_median(h, 0) == 0); |
| 97 |
test_history_time = 2; |
| 98 |
g_assert(gutil_int_history_add(h, 1) == 1); |
| 99 |
/* Time hasn't changed, 1 is replaced with 2 */ |
| 100 |
g_assert(gutil_int_history_add(h, 2) == 2); |
| 101 |
/* Time goes back, 2 is replaced with 3 */ |
| 102 |
test_history_time -=1; |
| 103 |
g_assert(gutil_int_history_add(h, 3) == 3); |
| 104 |
test_history_time += 2; |
| 105 |
g_assert(gutil_int_history_add(h, 5) == 4); |
| 106 |
g_assert(gutil_int_history_size(h) == 2); |
| 107 |
g_assert(gutil_int_history_median(h, 0) == 4); |
| 108 |
g_assert(gutil_int_history_interval(h) == 1); |
| 109 |
gutil_int_history_clear(h); |
| 110 |
g_assert(!gutil_int_history_size(h)); |
| 111 |
g_assert(!gutil_int_history_interval(h)); |
| 112 |
gutil_int_history_unref(h); |
| 113 |
} |
| 114 |
|
| 115 |
/*==========================================================================* |
| 116 |
* Median |
| 117 |
*==========================================================================*/ |
| 118 |
|
| 119 |
static |
| 120 |
void |
| 121 |
test_history_median( |
| 122 |
void) |
| 123 |
{ |
| 124 |
GUtilIntHistory* h = gutil_int_history_new_full(2, 2, |
| 125 |
test_history_time_func); |
| 126 |
test_history_time = 1; |
| 127 |
g_assert(gutil_int_history_add(h, 1) == 1); |
| 128 |
test_history_time += 1; |
| 129 |
g_assert(gutil_int_history_add(h, 5) == 3); |
| 130 |
test_history_time += 1; |
| 131 |
/* Still the same, both ends of the interval are inclusive */ |
| 132 |
g_assert(gutil_int_history_median(h, 0) == 3); |
| 133 |
test_history_time += 1; |
| 134 |
/* Now one entry expires */ |
| 135 |
g_assert(gutil_int_history_median(h, 0) == 5); |
| 136 |
test_history_time += 1; |
| 137 |
/* The last one expires too */ |
| 138 |
g_assert(gutil_int_history_median(h, 0) == 0); |
| 139 |
gutil_int_history_unref(h); |
| 140 |
} |
| 141 |
|
| 142 |
/*==========================================================================* |
| 143 |
* Size |
| 144 |
*==========================================================================*/ |
| 145 |
|
| 146 |
static |
| 147 |
void |
| 148 |
test_history_size( |
| 149 |
void) |
| 150 |
{ |
| 151 |
GUtilIntHistory* h = gutil_int_history_new_full(2, 2, |
| 152 |
test_history_time_func); |
| 153 |
test_history_time = 1; |
| 154 |
g_assert(gutil_int_history_add(h, 1) == 1); |
| 155 |
test_history_time += 1; |
| 156 |
g_assert(gutil_int_history_add(h, 5) == 3); |
| 157 |
g_assert(gutil_int_history_size(h) == 2); |
| 158 |
test_history_time += 1; |
| 159 |
/* Still the same, both ends of the interval are inclusive */ |
| 160 |
g_assert(gutil_int_history_size(h) == 2); |
| 161 |
test_history_time += 1; |
| 162 |
/* Now one entry expires */ |
| 163 |
g_assert(gutil_int_history_size(h) == 1); |
| 164 |
test_history_time += 1; |
| 165 |
/* The last one expires too */ |
| 166 |
g_assert(gutil_int_history_size(h) == 0); |
| 167 |
gutil_int_history_unref(h); |
| 168 |
} |
| 169 |
|
| 170 |
/*==========================================================================* |
| 171 |
* Interval |
| 172 |
*==========================================================================*/ |
| 173 |
|
| 174 |
static |
| 175 |
void |
| 176 |
test_history_interval( |
| 177 |
void) |
| 178 |
{ |
| 179 |
GUtilIntHistory* h = gutil_int_history_new_full(2, 3, |
| 180 |
test_history_time_func); |
| 181 |
test_history_time = 1; |
| 182 |
g_assert(gutil_int_history_add(h, 1) == 1); |
| 183 |
test_history_time += 1; |
| 184 |
g_assert(gutil_int_history_add(h, 5) == 3); |
| 185 |
g_assert(gutil_int_history_interval(h) == 1); |
| 186 |
test_history_time += 1; |
| 187 |
/* Both ends of the interval are inclusive */ |
| 188 |
g_assert(gutil_int_history_interval(h) == 2); |
| 189 |
test_history_time += 2; |
| 190 |
/* Now one entry with timestamp 1 expires, the one remaining has |
| 191 |
* timestamp 2, the current time is 5 so the interval becomes 3 */ |
| 192 |
g_assert(gutil_int_history_interval(h) == 3); |
| 193 |
test_history_time += 1; |
| 194 |
/* The last one expires too */ |
| 195 |
g_assert(gutil_int_history_interval(h) == 0); |
| 196 |
gutil_int_history_unref(h); |
| 197 |
} |
| 198 |
|
| 199 |
/*==========================================================================* |
| 200 |
* Data |
| 201 |
*==========================================================================*/ |
| 202 |
|
| 203 |
typedef struct test_history_sample { |
| 204 |
gint64 time; |
| 205 |
int value; |
| 206 |
int median; |
| 207 |
} TestHistorySample; |
| 208 |
|
| 209 |
typedef struct test_history_data { |
| 210 |
int max_size; |
| 211 |
gint64 max_interval; |
| 212 |
const TestHistorySample* samples; |
| 213 |
int count; |
| 214 |
guint size; |
| 215 |
gint64 interval; |
| 216 |
} TestHistoryData; |
| 217 |
|
| 218 |
static |
| 219 |
void |
| 220 |
test_history_data( |
| 221 |
gconstpointer test_data) |
| 222 |
{ |
| 223 |
int i; |
| 224 |
const TestHistoryData* data = test_data; |
| 225 |
GUtilIntHistory* h = gutil_int_history_new_full(data->max_size, |
| 226 |
data->max_interval, test_history_time_func); |
| 227 |
for (i=0; i<data->count; i++) { |
| 228 |
const TestHistorySample* sample = data->samples + i; |
| 229 |
test_history_time = sample->time; |
| 230 |
g_assert(gutil_int_history_add(h, sample->value) == sample->median); |
| 231 |
} |
| 232 |
g_assert(gutil_int_history_size(h) == data->size); |
| 233 |
g_assert(gutil_int_history_interval(h) == data->interval); |
| 234 |
test_history_time += data->max_interval + 1; |
| 235 |
g_assert(!gutil_int_history_size(h)); |
| 236 |
g_assert(!gutil_int_history_interval(h)); |
| 237 |
gutil_int_history_unref(h); |
| 238 |
} |
| 239 |
|
| 240 |
/*==========================================================================* |
| 241 |
* Common |
| 242 |
*==========================================================================*/ |
| 243 |
static const TestHistorySample samples1[] = {{1, 1, 1}}; |
| 244 |
static const TestHistoryData data1 = { |
| 245 |
1, 1, samples1, G_N_ELEMENTS(samples1), 1, 0 |
| 246 |
}; |
| 247 |
|
| 248 |
static const TestHistorySample samples2[] = {{1, 1, 1}, {3, 2, 2}}; |
| 249 |
static const TestHistoryData data2 = { |
| 250 |
1, 1, samples2, G_N_ELEMENTS(samples2), 1, 0 |
| 251 |
}; |
| 252 |
|
| 253 |
static const TestHistorySample samples3[] = { |
| 254 |
{1, 2, 2}, {3, 2, 2}, {4, 8, 3} |
| 255 |
}; |
| 256 |
static const TestHistoryData data3 = { |
| 257 |
3, 4, samples3, G_N_ELEMENTS(samples3), 3, 3 |
| 258 |
}; |
| 259 |
|
| 260 |
static const TestHistorySample samples4[] = { |
| 261 |
{1, 2, 2}, {3, 2, 2}, {4, 8, 5} |
| 262 |
}; |
| 263 |
static const TestHistoryData data4 = { |
| 264 |
3, 2, samples4, G_N_ELEMENTS(samples4), 2, 1 |
| 265 |
}; |
| 266 |
|
| 267 |
static const TestHistorySample samples5[] = { |
| 268 |
{1, 2, 2}, {3, 2, 2}, {4, 8, 5} |
| 269 |
}; |
| 270 |
static const TestHistoryData data5 = { |
| 271 |
2, 4, samples5, G_N_ELEMENTS(samples5), 2, 1 |
| 272 |
}; |
| 273 |
|
| 274 |
#define TEST_PREFIX "/history/" |
| 275 |
|
| 276 |
int main(int argc, char* argv[]) |
| 277 |
{ |
| 278 |
g_test_init(&argc, &argv, NULL); |
| 279 |
g_test_add_func(TEST_PREFIX "null", test_history_null); |
| 280 |
g_test_add_func(TEST_PREFIX "basic", test_history_basic); |
| 281 |
g_test_add_func(TEST_PREFIX "clear", test_history_clear); |
| 282 |
g_test_add_func(TEST_PREFIX "median", test_history_median); |
| 283 |
g_test_add_func(TEST_PREFIX "size", test_history_size); |
| 284 |
g_test_add_func(TEST_PREFIX "interval", test_history_interval); |
| 285 |
g_test_add_data_func(TEST_PREFIX "data1", &data1, test_history_data); |
| 286 |
g_test_add_data_func(TEST_PREFIX "data2", &data2, test_history_data); |
| 287 |
g_test_add_data_func(TEST_PREFIX "data3", &data3, test_history_data); |
| 288 |
g_test_add_data_func(TEST_PREFIX "data4", &data4, test_history_data); |
| 289 |
g_test_add_data_func(TEST_PREFIX "data5", &data5, test_history_data); |
| 290 |
test_init(&test_opt, argc, argv); |
| 291 |
return g_test_run(); |
| 292 |
} |
| 293 |
|
| 294 |
/* |
| 295 |
* Local Variables: |
| 296 |
* mode: C |
| 297 |
* c-basic-offset: 4 |
| 298 |
* indent-tabs-mode: nil |
| 299 |
* End: |
| 300 |
*/ |