1226031Sstas/*
2226031Sstas * Copyright (c) 2010 Kungliga Tekniska H��gskolan
3226031Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4226031Sstas * All rights reserved.
5226031Sstas *
6226031Sstas * Portions Copyright (c) 2010 Apple Inc. All rights reserved.
7226031Sstas *
8226031Sstas * Redistribution and use in source and binary forms, with or without
9226031Sstas * modification, are permitted provided that the following conditions
10226031Sstas * are met:
11226031Sstas *
12226031Sstas * 1. Redistributions of source code must retain the above copyright
13226031Sstas *    notice, this list of conditions and the following disclaimer.
14226031Sstas *
15226031Sstas * 2. Redistributions in binary form must reproduce the above copyright
16226031Sstas *    notice, this list of conditions and the following disclaimer in the
17226031Sstas *    documentation and/or other materials provided with the distribution.
18226031Sstas *
19226031Sstas * 3. Neither the name of the Institute nor the names of its contributors
20226031Sstas *    may be used to endorse or promote products derived from this software
21226031Sstas *    without specific prior written permission.
22226031Sstas *
23226031Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24226031Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25226031Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26226031Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27226031Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28226031Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29226031Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30226031Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31226031Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32226031Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33226031Sstas * SUCH DAMAGE.
34226031Sstas */
35226031Sstas
36226031Sstas#include <stdio.h>
37226031Sstas#include <err.h>
38226031Sstas
39226031Sstas#include "heimbase.h"
40226031Sstas#include "heimbasepriv.h"
41226031Sstas
42226031Sstasstatic void
43226031Sstasmemory_free(heim_object_t obj)
44226031Sstas{
45226031Sstas}
46226031Sstas
47226031Sstasstatic int
48226031Sstastest_memory(void)
49226031Sstas{
50226031Sstas    void *ptr;
51226031Sstas
52226031Sstas    ptr = heim_alloc(10, "memory", memory_free);
53226031Sstas
54226031Sstas    heim_retain(ptr);
55226031Sstas    heim_release(ptr);
56226031Sstas
57226031Sstas    heim_retain(ptr);
58226031Sstas    heim_release(ptr);
59226031Sstas
60226031Sstas    heim_release(ptr);
61226031Sstas
62226031Sstas    ptr = heim_alloc(10, "memory", NULL);
63226031Sstas    heim_release(ptr);
64226031Sstas
65226031Sstas    return 0;
66226031Sstas}
67226031Sstas
68226031Sstasstatic int
69226031Sstastest_dict(void)
70226031Sstas{
71226031Sstas    heim_dict_t dict;
72226031Sstas    heim_number_t a1 = heim_number_create(1);
73226031Sstas    heim_string_t a2 = heim_string_create("hejsan");
74226031Sstas    heim_number_t a3 = heim_number_create(3);
75226031Sstas    heim_string_t a4 = heim_string_create("foosan");
76226031Sstas
77226031Sstas    dict = heim_dict_create(10);
78226031Sstas
79226031Sstas    heim_dict_add_value(dict, a1, a2);
80226031Sstas    heim_dict_add_value(dict, a3, a4);
81226031Sstas
82226031Sstas    heim_dict_delete_key(dict, a3);
83226031Sstas    heim_dict_delete_key(dict, a1);
84226031Sstas
85226031Sstas    heim_release(a1);
86226031Sstas    heim_release(a2);
87226031Sstas    heim_release(a3);
88226031Sstas    heim_release(a4);
89226031Sstas
90226031Sstas    heim_release(dict);
91226031Sstas
92226031Sstas    return 0;
93226031Sstas}
94226031Sstas
95226031Sstasstatic int
96226031Sstastest_auto_release(void)
97226031Sstas{
98226031Sstas    heim_auto_release_t ar1, ar2;
99226031Sstas    heim_number_t n1;
100226031Sstas    heim_string_t s1;
101226031Sstas
102226031Sstas    ar1 = heim_auto_release_create();
103226031Sstas
104226031Sstas    s1 = heim_string_create("hejsan");
105226031Sstas    heim_auto_release(s1);
106226031Sstas
107226031Sstas    n1 = heim_number_create(1);
108226031Sstas    heim_auto_release(n1);
109226031Sstas
110226031Sstas    ar2 = heim_auto_release_create();
111226031Sstas
112226031Sstas    n1 = heim_number_create(1);
113226031Sstas    heim_auto_release(n1);
114226031Sstas
115226031Sstas    heim_release(ar2);
116226031Sstas    heim_release(ar1);
117226031Sstas
118226031Sstas    return 0;
119226031Sstas}
120226031Sstas
121226031Sstasstatic int
122226031Sstastest_string(void)
123226031Sstas{
124226031Sstas    heim_string_t s1, s2;
125226031Sstas    const char *string = "hejsan";
126226031Sstas
127226031Sstas    s1 = heim_string_create(string);
128226031Sstas    s2 = heim_string_create(string);
129226031Sstas
130226128Sstas    if (heim_cmp(s1, s2) != 0) {
131226128Sstas	printf("the same string is not the same\n");
132226128Sstas	exit(1);
133226128Sstas    }
134226031Sstas
135226031Sstas    heim_release(s1);
136226031Sstas    heim_release(s2);
137226031Sstas
138226031Sstas    return 0;
139226031Sstas}
140226031Sstas
141226031Sstasint
142226031Sstasmain(int argc, char **argv)
143226031Sstas{
144226031Sstas    int res = 0;
145226031Sstas
146226031Sstas    res |= test_memory();
147226031Sstas    res |= test_dict();
148226031Sstas    res |= test_auto_release();
149226031Sstas    res |= test_string();
150226031Sstas
151226031Sstas    return res;
152226031Sstas}
153