aboutsummaryrefslogtreecommitdiff
path: root/tests/test_memlimit.c
blob: 166e45b5bdc3348e11d234b941a9294995215ae4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
///////////////////////////////////////////////////////////////////////////////
//
/// \file       test_memlimit.c
/// \brief      Tests the memory usage limiter
///
/// \note       These tests cannot be done at exact byte count accuracy,
///             because memory limiter takes into account the memory wasted
///             by bookkeeping structures and alignment (padding).
//
//  Copyright (C) 2008 Lasse Collin
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
///////////////////////////////////////////////////////////////////////////////

#include "tests.h"


int
main(void)
{
	void *a;
	void *b;
	lzma_memlimit *mem;

	expect((mem = lzma_memlimit_create(1 << 16)) != NULL);
	expect(lzma_memlimit_count(mem) == 0);
	expect(lzma_memlimit_used(mem) > 0);
	expect(lzma_memlimit_used(mem) < 4096);
	expect(lzma_memlimit_used(mem) == lzma_memlimit_max(mem, false));
	expect(!lzma_memlimit_reached(mem, false));

	expect((a = lzma_memlimit_alloc(mem, 1, 4096)) != NULL);
	expect(lzma_memlimit_count(mem) == 1);
	expect(lzma_memlimit_used(mem) > 4096);
	expect(lzma_memlimit_used(mem) < 8192);
	expect(lzma_memlimit_used(mem) == lzma_memlimit_max(mem, false));
	expect(!lzma_memlimit_reached(mem, false));

	expect((b = lzma_memlimit_alloc(mem, 1, 4096)) != NULL);
	expect(lzma_memlimit_count(mem) == 2);
	expect(lzma_memlimit_used(mem) > 8192);
	expect(lzma_memlimit_used(mem) < 12288);
	expect(lzma_memlimit_used(mem) == lzma_memlimit_max(mem, false));
	expect(!lzma_memlimit_reached(mem, false));

	expect((lzma_memlimit_alloc(mem, 1, 1 << 17)) == NULL);
	expect(lzma_memlimit_count(mem) == 2);
	expect(lzma_memlimit_used(mem) > 8192);
	expect(lzma_memlimit_used(mem) < 12288);
	expect(lzma_memlimit_used(mem) < lzma_memlimit_max(mem, false));
	expect(lzma_memlimit_max(mem, false) > (1 << 17));
	expect(lzma_memlimit_reached(mem, false));

	lzma_memlimit_free(mem, a);
	expect(lzma_memlimit_count(mem) == 1);
	expect(lzma_memlimit_used(mem) > 4096);
	expect(lzma_memlimit_used(mem) < 8192);
	expect(lzma_memlimit_max(mem, true) > (1 << 17));
	expect(lzma_memlimit_reached(mem, true));
	expect(lzma_memlimit_used(mem) == lzma_memlimit_max(mem, false));
	expect(!lzma_memlimit_reached(mem, false));

	expect(lzma_memlimit_get(mem) == 1 << 16);
	lzma_memlimit_set(mem, 6144);
	expect(lzma_memlimit_get(mem) == 6144);
	expect(lzma_memlimit_alloc(mem, 1, 4096) == NULL);
	expect(lzma_memlimit_max(mem, false) > 8192);
	expect(lzma_memlimit_reached(mem, false));

	lzma_memlimit_free(mem, b);
	expect(lzma_memlimit_count(mem) == 0);
	expect(lzma_memlimit_used(mem) > 0);
	expect(lzma_memlimit_used(mem) < 4096);

	expect((a = lzma_memlimit_alloc(mem, 1, 4096)) != NULL);
	expect(lzma_memlimit_count(mem) == 1);
	expect(lzma_memlimit_used(mem) > 4096);
	expect(lzma_memlimit_used(mem) < 8192);

	expect(lzma_memlimit_max(mem, false) > 8192);
	expect(lzma_memlimit_reached(mem, false));
	expect(lzma_memlimit_max(mem, true) > 8192);
	expect(lzma_memlimit_reached(mem, true));
	expect(lzma_memlimit_max(mem, true) < 8192);
	expect(!lzma_memlimit_reached(mem, true));

	lzma_memlimit_detach(mem, a);
	free(a);
	expect(lzma_memlimit_count(mem) == 0);

	lzma_memlimit_set(mem, SIZE_MAX);
	expect(lzma_memlimit_alloc(mem, 1, SIZE_MAX - 33) == NULL);
	expect(lzma_memlimit_count(mem) == 0);
	expect(lzma_memlimit_max(mem, true) == SIZE_MAX);
	expect(lzma_memlimit_reached(mem, true));

	expect(lzma_memlimit_alloc(mem, 1, SIZE_MAX) == NULL);
	expect(lzma_memlimit_count(mem) == 0);
	expect(lzma_memlimit_max(mem, false) == SIZE_MAX);
	expect(lzma_memlimit_reached(mem, false));

	lzma_memlimit_end(mem, true);

	return 0;
}