mem_hdr_test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 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 "Generic.h"
13 #include "mem_node.h"
14 #include "stmem.h"
15 
16 #include <iostream>
17 #include <sstream>
18 
19 static void
21 {
22  mem_hdr aHeader;
23  assert (aHeader.lowestOffset() == 0);
24  assert (aHeader.write (StoreIOBuffer()));
25  assert (aHeader.lowestOffset() == 0);
26  assert (aHeader.write (StoreIOBuffer(0, 1, (char *)nullptr)));
27  assert (aHeader.lowestOffset() == 0);
28  char * sampleData = xstrdup ("A");
29  assert (aHeader.write (StoreIOBuffer(1, 100, sampleData)));
30  safe_free (sampleData);
31  assert (aHeader.lowestOffset() == 100);
32  assert (aHeader.endOffset() == 101);
33  sampleData = xstrdup ("B");
34  assert (aHeader.write (StoreIOBuffer(1, 10, sampleData)));
35  safe_free (sampleData);
36  assert (aHeader.lowestOffset() == 10);
37  assert (aHeader.endOffset() == 101);
41 }
42 
43 static void
45 {
46  Splay<mem_node *> aSplay;
47  mem_node *temp5;
48  temp5 = new mem_node(5);
49  temp5->nodeBuffer.length = 10;
50  aSplay.insert (temp5, mem_hdr::NodeCompare);
51  assert (aSplay.start()->data == temp5);
52  assert (aSplay.finish()->data == temp5);
53 
54  mem_node *temp0;
55  temp0 = new mem_node(0);
56  temp0->nodeBuffer.length = 5;
57  aSplay.insert (temp0, mem_hdr::NodeCompare);
58  assert (aSplay.start()->data == temp0);
59  assert (aSplay.finish()->data == temp5);
60 
61  mem_node *temp14;
62  temp14 = new mem_node (14);
63  temp14->nodeBuffer.length = 1;
64  assert (aSplay.find(temp14,mem_hdr::NodeCompare));
65  delete temp14;
66 
67  mem_node ref13 (13);
68  assert (!aSplay.find(&ref13,mem_hdr::NodeCompare));
69  ref13.nodeBuffer.length = 1;
70  assert (aSplay.find(&ref13,mem_hdr::NodeCompare));
71  aSplay.destroy();
72 }
73 
74 static void
76 {
77  mem_hdr aHeader;
78  char * sampleData = xstrdup ("A");
79  assert (aHeader.write (StoreIOBuffer(1, 100, sampleData)));
80  safe_free (sampleData);
81  sampleData = xstrdup ("B");
82  assert (aHeader.write (StoreIOBuffer(1, 102, sampleData)));
83  safe_free (sampleData);
84  std::ostringstream result;
85  PointerPrinter<mem_node *> foo(result, "\n");
86  for_each (aHeader.getNodes().end(), aHeader.getNodes().end(), foo);
87  for_each (aHeader.getNodes().begin(), aHeader.getNodes().begin(), foo);
88  for_each (aHeader.getNodes().begin(), aHeader.getNodes().end(), foo);
89  std::ostringstream expectedResult;
90  expectedResult << "[100,101)" << std::endl << "[102,103)" << std::endl;
91  assert (result.str() == expectedResult.str());
92 }
93 
94 int
95 main(int, char *[])
96 {
101  assert (mem_node::InUseCount() == 0);
102  testHdrVisit();
103  assert (mem_node::InUseCount() == 0);
104  return EXIT_SUCCESS;
105 }
106 
const SplayNode< V > * start() const
Definition: splay.h:349
static Splay< mem_node * >::SPLAYCMP NodeCompare
Definition: stmem.h:42
const Value * find(FindValue const &, int(*compare)(FindValue const &a, Value const &b)) const
Definition: splay.h:305
StoreIOBuffer nodeBuffer
Definition: mem_node.h:35
const_iterator begin() const
Definition: splay.h:388
#define xstrdup
bool write(StoreIOBuffer const &)
Definition: stmem.cc:303
const Splay< mem_node * > & getNodes() const
Definition: stmem.cc:372
int64_t endOffset() const
Definition: stmem.cc:45
Definition: Range.h:18
int main(int, char *[])
Definition: mem_hdr_test.cc:95
int64_t lowestOffset() const
Definition: stmem.cc:34
Definition: stmem.h:19
#define safe_free(x)
Definition: xalloc.h:73
#define assert(EX)
Definition: assert.h:17
const_iterator end() const
Definition: splay.h:395
void destroy(SPLAYFREE *=DefaultFree)
Definition: splay.h:369
static size_t InUseCount()
Definition: mem_node.cc:58
const SplayNode< V > * finish() const
Definition: splay.h:359
Value data
Definition: splay.h:25
bool hasContigousContentRange(Range< int64_t > const &range) const
Definition: stmem.cc:245
static void testLowAndHigh()
Definition: mem_hdr_test.cc:20
const Value * insert(const Value &, SPLAYCMP *)
Definition: splay.h:320
static void testSplayOfNodes()
Definition: mem_hdr_test.cc:44
static void testHdrVisit()
Definition: mem_hdr_test.cc:75
T & for_each(L const &head, T &visitor)
Definition: Generic.h:23

 

Introduction

Documentation

Support

Miscellaneous