]> git.ipfire.org Git - thirdparty/squid.git/blob - test-suite/mem_hdr_test.cc
Summary: Implement mem_hdr debugging.
[thirdparty/squid.git] / test-suite / mem_hdr_test.cc
1
2 /*
3 * $Id: mem_hdr_test.cc,v 1.5 2003/09/22 08:50:51 robertc Exp $
4 *
5 * DEBUG: section 19 Store Memory Primitives
6 * AUTHOR: Robert Collins
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 * Copyright (c) 2003 Robert Collins <robertc@squid-cache.org>
35 */
36
37 #include "squid.h"
38 #include "stmem.h"
39 #include "mem_node.h"
40 #include <iostream>
41 #include "Generic.h"
42
43 void
44 testLowAndHigh()
45 {
46 mem_hdr aHeader;
47 assert (aHeader.lowestOffset() == 0);
48 assert (aHeader.write (StoreIOBuffer()));
49 assert (aHeader.lowestOffset() == 0);
50 assert (aHeader.write (StoreIOBuffer(0, 1, NULL)));
51 assert (aHeader.lowestOffset() == 0);
52 char * sampleData = xstrdup ("A");
53 assert (aHeader.write (StoreIOBuffer(1, 100, sampleData)));
54 safe_free (sampleData);
55 assert (aHeader.lowestOffset() == 100);
56 assert (aHeader.endOffset() == 101);
57 sampleData = xstrdup ("B");
58 assert (aHeader.write (StoreIOBuffer(1, 10, sampleData)));
59 safe_free (sampleData);
60 assert (aHeader.lowestOffset() == 10);
61 assert (aHeader.endOffset() == 101);
62 assert (aHeader.hasContigousContentRange(Range<size_t>(10,11)));
63 assert (!aHeader.hasContigousContentRange(Range<size_t>(10,12)));
64 assert (!aHeader.hasContigousContentRange(Range<size_t>(10,101)));
65 }
66
67 void
68 testSplayOfNodes()
69 {
70 Splay<mem_node *> aSplay;
71 mem_node *temp5;
72 temp5 = new mem_node(5);
73 temp5->nodeBuffer.length = 10;
74 aSplay.insert (temp5, mem_hdr::NodeCompare);
75 assert (aSplay.start()->data == temp5);
76 assert (aSplay.finish()->data == temp5);
77
78 mem_node *temp0;
79 temp0 = new mem_node(0);
80 temp0->nodeBuffer.length = 5;
81 aSplay.insert (temp0, mem_hdr::NodeCompare);
82 assert (aSplay.start()->data == temp0);
83 assert (aSplay.finish()->data == temp5);
84
85 mem_node *temp14;
86 temp14 = new mem_node (14);
87 temp14->nodeBuffer.length = 1;
88 assert (aSplay.find(temp14,mem_hdr::NodeCompare));
89 delete temp14;
90
91 mem_node ref13 (13);
92 assert (!aSplay.find(&ref13,mem_hdr::NodeCompare));
93 ref13.nodeBuffer.length = 1;
94 assert (aSplay.find(&ref13,mem_hdr::NodeCompare));
95 aSplay.destroy(SplayNode<mem_node *>::DefaultFree);
96 }
97
98 void
99 testHdrVisit()
100 {
101 mem_hdr aHeader;
102 char * sampleData = xstrdup ("A");
103 assert (aHeader.write (StoreIOBuffer(1, 100, sampleData)));
104 safe_free (sampleData);
105 sampleData = xstrdup ("B");
106 assert (aHeader.write (StoreIOBuffer(1, 102, sampleData)));
107 safe_free (sampleData);
108 std::ostringstream result;
109 PointerPrinter<mem_node *> foo(result, "\n");
110 for_each (aHeader.getNodes().end(), aHeader.getNodes().end(), foo);
111 for_each (aHeader.getNodes().begin(), aHeader.getNodes().begin(), foo);
112 for_each (aHeader.getNodes().begin(), aHeader.getNodes().end(), foo);
113 std::ostringstream expectedResult;
114 expectedResult << "[100,101)" << std::endl << "[102,103)" << std::endl;
115 assert (result.str() == expectedResult.str());
116 }
117
118 int
119 main (int argc, char *argv)
120 {
121 assert (mem_node::InUseCount() == 0);
122 testLowAndHigh();
123 assert (mem_node::InUseCount() == 0);
124 testSplayOfNodes();
125 assert (mem_node::InUseCount() == 0);
126 testHdrVisit();
127 assert (mem_node::InUseCount() == 0);
128 return 0;
129 }