next_free.c (2516B)
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20#include <CUnit/CUnit.h> 21#include <guacamole/pool.h> 22 23/** 24 * The number of unique integers to provide through the guac_pool instance 25 * being tested. 26 */ 27#define POOL_SIZE 128 28 29/** 30 * Test which verifies that guac_pool provides access to a given number of 31 * unique integers, never repeating a retrieved integer until that integer 32 * is returned to the pool. 33 */ 34void test_pool__next_free() { 35 36 guac_pool* pool; 37 38 int i; 39 int seen[POOL_SIZE] = {0}; 40 int value; 41 42 /* Get pool */ 43 pool = guac_pool_alloc(POOL_SIZE); 44 CU_ASSERT_PTR_NOT_NULL_FATAL(pool); 45 46 /* Fill pool */ 47 for (i=0; i<POOL_SIZE; i++) { 48 49 /* Get value from pool */ 50 value = guac_pool_next_int(pool); 51 52 /* Value should be within pool size */ 53 CU_ASSERT_FATAL(value >= 0); 54 CU_ASSERT_FATAL(value < POOL_SIZE); 55 56 /* This should be an integer we have not seen yet */ 57 CU_ASSERT_EQUAL(0, seen[value]); 58 seen[value]++; 59 60 /* Return value to pool */ 61 guac_pool_free_int(pool, value); 62 63 } 64 65 /* Now that pool is filled, we should get ONLY previously seen integers */ 66 for (i=0; i<POOL_SIZE; i++) { 67 68 /* Get value from pool */ 69 value = guac_pool_next_int(pool); 70 71 /* Value should be within pool size */ 72 CU_ASSERT_FATAL(value >= 0); 73 CU_ASSERT_FATAL(value < POOL_SIZE); 74 75 /* This should be an integer we have seen only once */ 76 CU_ASSERT_EQUAL(1, seen[value]); 77 seen[value]++; 78 79 } 80 81 /* Pool is filled to minimum now. Next value should be equal to size. */ 82 value = guac_pool_next_int(pool); 83 84 CU_ASSERT_EQUAL(POOL_SIZE, value); 85 86 /* Free pool */ 87 guac_pool_free(pool); 88 89} 90