cscg24-guacamole

CSCG 2024 Challenge 'Guacamole Mashup'
git clone https://git.sinitax.com/sinitax/cscg24-guacamole
Log | Files | Refs | sfeed.txt

buffer_pool.c (2384B)


      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/client.h>
     22#include <guacamole/layer.h>
     23
     24#include <stdbool.h>
     25
     26/**
     27 * Test which verifies that buffers can be allocated and freed using the pool
     28 * of buffers available to each guac_client, and that doing so does not disturb
     29 * the similar pool of layers.
     30 */
     31void test_client__buffer_pool() {
     32
     33    guac_client* client;
     34
     35    int i;
     36    bool seen[GUAC_BUFFER_POOL_INITIAL_SIZE] = { 0 };
     37
     38    guac_layer* layer;
     39
     40    /* Get client */
     41    client = guac_client_alloc();
     42    CU_ASSERT_PTR_NOT_NULL_FATAL(client);
     43
     44    /* Fill pool */
     45    for (i=0; i<GUAC_BUFFER_POOL_INITIAL_SIZE; i++) {
     46
     47        /* Allocate and throw away a layer (should not disturb buffer alloc) */
     48        CU_ASSERT_PTR_NOT_NULL_FATAL(guac_client_alloc_layer(client));
     49
     50        layer = guac_client_alloc_buffer(client);
     51
     52        /* Index should be within pool size */
     53        CU_ASSERT_PTR_NOT_NULL_FATAL(layer);
     54        CU_ASSERT_FATAL(layer->index < 0);
     55        CU_ASSERT_FATAL(layer->index >= -GUAC_BUFFER_POOL_INITIAL_SIZE);
     56
     57        /* This should be a layer we have not seen yet */
     58        CU_ASSERT_FALSE(seen[-layer->index - 1]);
     59        seen[-layer->index - 1] = true;
     60
     61        guac_client_free_buffer(client, layer);
     62
     63    }
     64
     65    /* Now that pool is filled, we should get a previously seen layer */
     66    layer = guac_client_alloc_buffer(client);
     67
     68    CU_ASSERT_FATAL(layer->index < 0);
     69    CU_ASSERT_FATAL(layer->index >= -GUAC_BUFFER_POOL_INITIAL_SIZE);
     70    CU_ASSERT_TRUE(seen[-layer->index - 1]);
     71
     72    /* Free client */
     73    guac_client_free(client);
     74
     75}
     76