mem_hdr_test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 /* DEBUG: section 19 Store Memory Primitives */
10 
11 #include "squid.h"
12 #include "base/TextException.h"
13 #include "Generic.h"
14 #include "mem_node.h"
15 #include "stmem.h"
16 
17 #include <iostream>
18 #include <sstream>
19 
20 // required on some platforms
21 unsigned int
22 TextException::FileNameHash(const char *)
23 {
24  return 0;
25 }
26 
27 void
29 {
30  mem_hdr aHeader;
31  assert (aHeader.lowestOffset() == 0);
32  assert (aHeader.write (StoreIOBuffer()));
33  assert (aHeader.lowestOffset() == 0);
34  assert (aHeader.write (StoreIOBuffer(0, 1, (char *)NULL)));
35  assert (aHeader.lowestOffset() == 0);
36  char * sampleData = xstrdup ("A");
37  assert (aHeader.write (StoreIOBuffer(1, 100, sampleData)));
38  safe_free (sampleData);
39  assert (aHeader.lowestOffset() == 100);
40  assert (aHeader.endOffset() == 101);
41  sampleData = xstrdup ("B");
42  assert (aHeader.write (StoreIOBuffer(1, 10, sampleData)));
43  safe_free (sampleData);
44  assert (aHeader.lowestOffset() == 10);
45  assert (aHeader.endOffset() == 101);
49 }
50 
51 void
53 {
54  Splay<mem_node *> aSplay;
55  mem_node *temp5;
56  temp5 = new mem_node(5);
57  temp5->nodeBuffer.length = 10;
58  aSplay.insert (temp5, mem_hdr::NodeCompare);
59  assert (aSplay.start()->data == temp5);
60  assert (aSplay.finish()->data == temp5);
61 
62  mem_node *temp0;
63  temp0 = new mem_node(0);
64  temp0->nodeBuffer.length = 5;
65  aSplay.insert (temp0, mem_hdr::NodeCompare);
66  assert (aSplay.start()->data == temp0);
67  assert (aSplay.finish()->data == temp5);
68 
69  mem_node *temp14;
70  temp14 = new mem_node (14);
71  temp14->nodeBuffer.length = 1;
72  assert (aSplay.find(temp14,mem_hdr::NodeCompare));
73  delete temp14;
74 
75  mem_node ref13 (13);
76  assert (!aSplay.find(&ref13,mem_hdr::NodeCompare));
77  ref13.nodeBuffer.length = 1;
78  assert (aSplay.find(&ref13,mem_hdr::NodeCompare));
79  aSplay.destroy();
80 }
81 
82 void
84 {
85  mem_hdr aHeader;
86  char * sampleData = xstrdup ("A");
87  assert (aHeader.write (StoreIOBuffer(1, 100, sampleData)));
88  safe_free (sampleData);
89  sampleData = xstrdup ("B");
90  assert (aHeader.write (StoreIOBuffer(1, 102, sampleData)));
91  safe_free (sampleData);
92  std::ostringstream result;
93  PointerPrinter<mem_node *> foo(result, "\n");
94  for_each (aHeader.getNodes().end(), aHeader.getNodes().end(), foo);
95  for_each (aHeader.getNodes().begin(), aHeader.getNodes().begin(), foo);
96  for_each (aHeader.getNodes().begin(), aHeader.getNodes().end(), foo);
97  std::ostringstream expectedResult;
98  expectedResult << "[100,101)" << std::endl << "[102,103)" << std::endl;
99  assert (result.str() == expectedResult.str());
100 }
101 
102 int
103 main(int, char *[])
104 {
105  assert (mem_node::InUseCount() == 0);
106  testLowAndHigh();
107  assert (mem_node::InUseCount() == 0);
109  assert (mem_node::InUseCount() == 0);
110  testHdrVisit();
111  assert (mem_node::InUseCount() == 0);
112  return EXIT_SUCCESS;
113 }
114 
void insert(Value const &, SPLAYCMP *compare)
Definition: splay.h:300
SplayNode< V > const * start() const
Definition: splay.h:327
int64_t endOffset() const
Definition: stmem.cc:46
#define assert(EX)
Definition: assert.h:17
const_iterator end() const
Definition: splay.h:373
void testLowAndHigh()
Definition: mem_hdr_test.cc:28
#define xstrdup
#define safe_free(x)
Definition: xalloc.h:73
Definition: Range.h:18
Value const * find(FindValue const &, int(*compare)(FindValue const &a, Value const &b)) const
Definition: stmem.h:19
int main(int, char *[])
T & for_each(L const &head, T &visitor)
Definition: Generic.h:23
int64_t lowestOffset() const
Definition: stmem.cc:35
SplayNode< V > const * finish() const
Definition: splay.h:337
bool write(StoreIOBuffer const &)
Definition: stmem.cc:342
static unsigned int FileNameHash(const char *fname)
a small integer hash value to semi-uniquely identify the source file
static size_t InUseCount()
Definition: mem_node.cc:55
const_iterator begin() const
Definition: splay.h:366
StoreIOBuffer nodeBuffer
Definition: mem_node.h:35
Value data
Definition: splay.h:27
void testHdrVisit()
Definition: mem_hdr_test.cc:83
static Splay< mem_node * >::SPLAYCMP NodeCompare
Definition: stmem.h:47
const Splay< mem_node * > & getNodes() const
Definition: stmem.cc:425
void destroy(SPLAYFREE *=SplayNode< V >::DefaultFree)
Definition: splay.h:347
bool hasContigousContentRange(Range< int64_t > const &range) const
Definition: stmem.cc:284
void testSplayOfNodes()
Definition: mem_hdr_test.cc:52
#define NULL
Definition: types.h:166

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors