test_base.c revision 226128
112795Swpaul/* 212795Swpaul * Copyright (c) 2010 Kungliga Tekniska H��gskolan 312795Swpaul * (Royal Institute of Technology, Stockholm, Sweden). 412795Swpaul * All rights reserved. 512795Swpaul * 612795Swpaul * Portions Copyright (c) 2010 Apple Inc. All rights reserved. 712795Swpaul * 8100441Scharnier * Redistribution and use in source and binary forms, with or without 912795Swpaul * modification, are permitted provided that the following conditions 1012795Swpaul * are met: 1112795Swpaul * 12100441Scharnier * 1. Redistributions of source code must retain the above copyright 1312795Swpaul * notice, this list of conditions and the following disclaimer. 1412795Swpaul * 1512795Swpaul * 2. Redistributions in binary form must reproduce the above copyright 16100441Scharnier * notice, this list of conditions and the following disclaimer in the 1712795Swpaul * documentation and/or other materials provided with the distribution. 1812795Swpaul * 1912795Swpaul * 3. Neither the name of the Institute nor the names of its contributors 20100441Scharnier * may be used to endorse or promote products derived from this software 2112795Swpaul * without specific prior written permission. 2212795Swpaul * 2312795Swpaul * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 24100441Scharnier * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2512795Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2612795Swpaul * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 2712795Swpaul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2812795Swpaul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2912795Swpaul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30100441Scharnier * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3112795Swpaul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32146833Sstefanf * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3312795Swpaul * SUCH DAMAGE. 3412795Swpaul */ 3527935Scharnier 3612795Swpaul#include <stdio.h> 37100441Scharnier#include <err.h> 38100441Scharnier 39100441Scharnier#include "heimbase.h" 4012795Swpaul#include "heimbasepriv.h" 4112795Swpaul 4212795Swpaulstatic void 4312795Swpaulmemory_free(heim_object_t obj) 4427935Scharnier{ 4512795Swpaul} 4612795Swpaul 4712795Swpaulstatic int 48149682Sstefanftest_memory(void) 4912795Swpaul{ 5012795Swpaul void *ptr; 5112795Swpaul 5212795Swpaul ptr = heim_alloc(10, "memory", memory_free); 5312795Swpaul 5412795Swpaul heim_retain(ptr); 5512795Swpaul heim_release(ptr); 5612795Swpaul 5712795Swpaul heim_retain(ptr); 58141614Salfred heim_release(ptr); 5912795Swpaul 60141614Salfred heim_release(ptr); 6112795Swpaul 62141614Salfred ptr = heim_alloc(10, "memory", NULL); 6312795Swpaul heim_release(ptr); 6412795Swpaul 6512795Swpaul return 0; 6612795Swpaul} 6792921Simp 6892921Simpstatic int 6992921Simptest_dict(void) 7012795Swpaul{ 7112795Swpaul heim_dict_t dict; 7212795Swpaul heim_number_t a1 = heim_number_create(1); 7312795Swpaul heim_string_t a2 = heim_string_create("hejsan"); 7412795Swpaul heim_number_t a3 = heim_number_create(3); 7512795Swpaul heim_string_t a4 = heim_string_create("foosan"); 7612795Swpaul 7712795Swpaul dict = heim_dict_create(10); 7812795Swpaul 7912795Swpaul heim_dict_add_value(dict, a1, a2); 8012795Swpaul heim_dict_add_value(dict, a3, a4); 8112795Swpaul 8212795Swpaul heim_dict_delete_key(dict, a3); 8312795Swpaul heim_dict_delete_key(dict, a1); 8412795Swpaul 8512795Swpaul heim_release(a1); 8617142Sjkh heim_release(a2); 8712795Swpaul heim_release(a3); 8812795Swpaul heim_release(a4); 8912795Swpaul 9012795Swpaul heim_release(dict); 9112795Swpaul 9212795Swpaul return 0; 9312795Swpaul} 9412795Swpaul 9512795Swpaulstatic int 9612795Swpaultest_auto_release(void) 9712795Swpaul{ 9812795Swpaul heim_auto_release_t ar1, ar2; 9912795Swpaul heim_number_t n1; 10012795Swpaul heim_string_t s1; 10112795Swpaul 10212795Swpaul ar1 = heim_auto_release_create(); 10312795Swpaul 10412795Swpaul s1 = heim_string_create("hejsan"); 10512795Swpaul heim_auto_release(s1); 10612795Swpaul 10712795Swpaul n1 = heim_number_create(1); 10812795Swpaul heim_auto_release(n1); 10912795Swpaul 11012795Swpaul ar2 = heim_auto_release_create(); 11112795Swpaul 11212795Swpaul n1 = heim_number_create(1); 11312795Swpaul heim_auto_release(n1); 11412795Swpaul 11512795Swpaul heim_release(ar2); 11627935Scharnier heim_release(ar1); 11712795Swpaul 11812795Swpaul return 0; 11912795Swpaul} 12012795Swpaul 12112795Swpaulstatic int 122141614Salfredtest_string(void) 12312795Swpaul{ 12412795Swpaul heim_string_t s1, s2; 12512795Swpaul const char *string = "hejsan"; 12612795Swpaul 12712795Swpaul s1 = heim_string_create(string); 12812795Swpaul s2 = heim_string_create(string); 12912795Swpaul 13012795Swpaul if (heim_cmp(s1, s2) != 0) { 13112795Swpaul printf("the same string is not the same\n"); 13212795Swpaul exit(1); 13312795Swpaul } 13412795Swpaul 13512795Swpaul heim_release(s1); 13612795Swpaul heim_release(s2); 137100441Scharnier 13812795Swpaul return 0; 13912795Swpaul} 14012795Swpaul 14112795Swpaulint 14212795Swpaulmain(int argc, char **argv) 14312795Swpaul{ 14412795Swpaul int res = 0; 14512795Swpaul 14612795Swpaul res |= test_memory(); 14712795Swpaul res |= test_dict(); 14812795Swpaul res |= test_auto_release(); 14912795Swpaul res |= test_string(); 15012795Swpaul 15117142Sjkh return res; 15212795Swpaul} 15312795Swpaul