]> icculus.org git repositories - icculus/xz.git/blob - tests/test_memlimit.c
Initial changes to change the suffix of the new format to .xz.
[icculus/xz.git] / tests / test_memlimit.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       test_memlimit.c
4 /// \brief      Tests the memory usage limiter
5 ///
6 /// \note       These tests cannot be done at exact byte count accuracy,
7 ///             because memory limiter takes into account the memory wasted
8 ///             by bookkeeping structures and alignment (padding).
9 //
10 //  Copyright (C) 2008 Lasse Collin
11 //
12 //  This library is free software; you can redistribute it and/or
13 //  modify it under the terms of the GNU Lesser General Public
14 //  License as published by the Free Software Foundation; either
15 //  version 2.1 of the License, or (at your option) any later version.
16 //
17 //  This library is distributed in the hope that it will be useful,
18 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
19 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 //  Lesser General Public License for more details.
21 //
22 ///////////////////////////////////////////////////////////////////////////////
23
24 #include "tests.h"
25
26
27 int
28 main(void)
29 {
30         void *a;
31         void *b;
32         lzma_memlimit *mem;
33
34         expect((mem = lzma_memlimit_create(1 << 16)) != NULL);
35         expect(lzma_memlimit_count(mem) == 0);
36         expect(lzma_memlimit_used(mem) > 0);
37         expect(lzma_memlimit_used(mem) < 4096);
38         expect(lzma_memlimit_used(mem) == lzma_memlimit_max(mem, false));
39         expect(!lzma_memlimit_reached(mem, false));
40
41         expect((a = lzma_memlimit_alloc(mem, 1, 4096)) != NULL);
42         expect(lzma_memlimit_count(mem) == 1);
43         expect(lzma_memlimit_used(mem) > 4096);
44         expect(lzma_memlimit_used(mem) < 8192);
45         expect(lzma_memlimit_used(mem) == lzma_memlimit_max(mem, false));
46         expect(!lzma_memlimit_reached(mem, false));
47
48         expect((b = lzma_memlimit_alloc(mem, 1, 4096)) != NULL);
49         expect(lzma_memlimit_count(mem) == 2);
50         expect(lzma_memlimit_used(mem) > 8192);
51         expect(lzma_memlimit_used(mem) < 12288);
52         expect(lzma_memlimit_used(mem) == lzma_memlimit_max(mem, false));
53         expect(!lzma_memlimit_reached(mem, false));
54
55         expect((lzma_memlimit_alloc(mem, 1, 1 << 17)) == NULL);
56         expect(lzma_memlimit_count(mem) == 2);
57         expect(lzma_memlimit_used(mem) > 8192);
58         expect(lzma_memlimit_used(mem) < 12288);
59         expect(lzma_memlimit_used(mem) < lzma_memlimit_max(mem, false));
60         expect(lzma_memlimit_max(mem, false) > (1 << 17));
61         expect(lzma_memlimit_reached(mem, false));
62
63         lzma_memlimit_free(mem, a);
64         expect(lzma_memlimit_count(mem) == 1);
65         expect(lzma_memlimit_used(mem) > 4096);
66         expect(lzma_memlimit_used(mem) < 8192);
67         expect(lzma_memlimit_max(mem, true) > (1 << 17));
68         expect(lzma_memlimit_reached(mem, true));
69         expect(lzma_memlimit_used(mem) == lzma_memlimit_max(mem, false));
70         expect(!lzma_memlimit_reached(mem, false));
71
72         expect(lzma_memlimit_get(mem) == 1 << 16);
73         lzma_memlimit_set(mem, 6144);
74         expect(lzma_memlimit_get(mem) == 6144);
75         expect(lzma_memlimit_alloc(mem, 1, 4096) == NULL);
76         expect(lzma_memlimit_max(mem, false) > 8192);
77         expect(lzma_memlimit_reached(mem, false));
78
79         lzma_memlimit_free(mem, b);
80         expect(lzma_memlimit_count(mem) == 0);
81         expect(lzma_memlimit_used(mem) > 0);
82         expect(lzma_memlimit_used(mem) < 4096);
83
84         expect((a = lzma_memlimit_alloc(mem, 1, 4096)) != NULL);
85         expect(lzma_memlimit_count(mem) == 1);
86         expect(lzma_memlimit_used(mem) > 4096);
87         expect(lzma_memlimit_used(mem) < 8192);
88
89         expect(lzma_memlimit_max(mem, false) > 8192);
90         expect(lzma_memlimit_reached(mem, false));
91         expect(lzma_memlimit_max(mem, true) > 8192);
92         expect(lzma_memlimit_reached(mem, true));
93         expect(lzma_memlimit_max(mem, true) < 8192);
94         expect(!lzma_memlimit_reached(mem, true));
95
96         lzma_memlimit_detach(mem, a);
97         free(a);
98         expect(lzma_memlimit_count(mem) == 0);
99
100         lzma_memlimit_set(mem, SIZE_MAX);
101         expect(lzma_memlimit_alloc(mem, 1, SIZE_MAX - 33) == NULL);
102         expect(lzma_memlimit_count(mem) == 0);
103         expect(lzma_memlimit_max(mem, true) == SIZE_MAX);
104         expect(lzma_memlimit_reached(mem, true));
105
106         expect(lzma_memlimit_alloc(mem, 1, SIZE_MAX) == NULL);
107         expect(lzma_memlimit_count(mem) == 0);
108         expect(lzma_memlimit_max(mem, false) == SIZE_MAX);
109         expect(lzma_memlimit_reached(mem, false));
110
111         lzma_memlimit_end(mem, true);
112
113         return 0;
114 }