"Fossies" - the Fresh Open Source Software Archive

Member "ftwin-master/check/check_napr_heap.c" (15 Feb 2015, 3036 Bytes) of package /linux/privat/ftwin-master.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  * Copyright (C) 2007 Fran├žois Pesce : francois.pesce (at) gmail (dot) com
    3  *
    4  * Licensed under the Apache License, Version 2.0 (the "License");
    5  * you may not use this file except in compliance with the License.
    6  * You may obtain a copy of the License at
    7  * 
    8  *  http://www.apache.org/licenses/LICENSE-2.0
    9  * 
   10  * Unless required by applicable law or agreed to in writing, software
   11  * distributed under the License is distributed on an "AS IS" BASIS,
   12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13  * See the License for the specific language governing permissions and
   14  * limitations under the License.
   15  */
   16 
   17 #include <unistd.h>
   18 #include <stdlib.h>
   19 #include <check.h>
   20 
   21 #include <apr_strings.h>
   22 
   23 #include "debug.h"
   24 #include "napr_heap.h"
   25 
   26 extern apr_pool_t *main_pool;
   27 apr_pool_t *pool;
   28 
   29 static void setup(void)
   30 {
   31     apr_status_t rs;
   32 
   33     rs = apr_pool_create(&pool, main_pool);
   34     if (rs != APR_SUCCESS) {
   35     DEBUG_ERR("Error creating pool");
   36     exit(1);
   37     }
   38 }
   39 
   40 static void teardown(void)
   41 {
   42     apr_pool_destroy(pool);
   43 }
   44 
   45 struct check_heap_numbers_t
   46 {
   47     apr_off_t size;
   48 };
   49 typedef struct check_heap_numbers_t check_heap_numbers_t;
   50 
   51 static int check_heap_numbers_cmp(const void *param1, const void *param2)
   52 {
   53     const check_heap_numbers_t *number1 = param1;
   54     const check_heap_numbers_t *number2 = param2;
   55 
   56     if (number1->size < number2->size) {
   57     return -1;
   58     }
   59     else if (number2->size < number1->size) {
   60     return 1;
   61     }
   62 
   63     return 0;
   64 }
   65 
   66 static void check_heap_numbers_display(const void *param)
   67 {
   68     const check_heap_numbers_t *number = param;
   69 
   70     printf("%" APR_OFF_T_FMT, number->size);
   71 }
   72 
   73 START_TEST(test_napr_heap_unordered_bug)
   74 {
   75     apr_off_t array[] = { 6298, 43601, 193288, 30460, 193288 };
   76     napr_heap_t *heap;
   77     check_heap_numbers_t *number;
   78     int i;
   79 
   80     heap = napr_heap_make_r(pool, check_heap_numbers_cmp);
   81     napr_heap_set_display_cb(heap, check_heap_numbers_display);
   82 
   83     for (i = 0; i < sizeof(array) / sizeof(apr_off_t); i++) {
   84     number = apr_palloc(pool, sizeof(struct check_heap_numbers_t));
   85     number->size = array[i];
   86     napr_heap_insert_r(heap, number);
   87     }
   88 
   89     for (i = 0; i < sizeof(array) / sizeof(apr_off_t); i++) {
   90     number = napr_heap_extract(heap);
   91     switch (i) {
   92     case 0:
   93         fail_unless(number->size == 193288, "first bad ordered");
   94         break;
   95     case 1:
   96         fail_unless(number->size == 193288, "second bad ordered");
   97         break;
   98     case 2:
   99         fail_unless(number->size == 43601, "third bad ordered");
  100         break;
  101     case 3:
  102         fail_unless(number->size == 30460, "fourth bad ordered");
  103         break;
  104     case 4:
  105         fail_unless(number->size == 6298, "fifth bad ordered");
  106         break;
  107     }
  108     }
  109 }
  110 /* *INDENT-OFF* */
  111 END_TEST
  112 /* *INDENT-ON* */
  113 
  114 Suite *make_napr_heap_suite(void)
  115 {
  116     Suite *s;
  117     TCase *tc_core;
  118     s = suite_create("Napr_Heap");
  119     tc_core = tcase_create("Core Tests");
  120 
  121     tcase_add_checked_fixture(tc_core, setup, teardown);
  122     tcase_add_test(tc_core, test_napr_heap_unordered_bug);
  123     suite_add_tcase(s, tc_core);
  124 
  125     return s;
  126 }