UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
test_dbuffer.cpp
Go to the documentation of this file.
1 
5 /*
6 Copyright (C) 2002-2020 UFO: Alien Invasion.
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 
17 See the GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 
23 */
24 
25 
26 #include "test_shared.h"
27 #include "../common/common.h"
28 
29 class DBufferTest: public ::testing::Test {
30 protected:
31  static void SetUpTestCase() {
32  TEST_Init();
33  }
34 
35  static void TearDownTestCase() {
36  TEST_Shutdown();
37  }
38 };
39 
40 TEST_F(DBufferTest, testDBuffer)
41 {
42  int i;
43  const int size = 10000000;
44  dbuffer* buf = new dbuffer();
45  char data[128];
46  size_t dataSize = sizeof(data);
47  for (i = 0; i < size; i++)
48  buf->add("b", 1);
49  ASSERT_EQ(size, buf->length());
50 
51  ASSERT_EQ(dataSize, buf->get(data, dataSize));
52  ASSERT_EQ(size, buf->length());
53 
54  ASSERT_EQ(dataSize, buf->extract(data, dataSize));
55  ASSERT_EQ(size - dataSize, buf->length());
56 
57  delete buf;
58 
59  buf = new dbuffer();
60  buf->add("b", 1);
61  ASSERT_EQ(1, buf->length());
62 
63  ASSERT_EQ(1, buf->get(data, dataSize));
64  ASSERT_EQ(1, buf->length());
65 
66  ASSERT_EQ(1, buf->extract(data, dataSize));
67  ASSERT_EQ(0, buf->length());
68 
69  dbuffer* dup = new dbuffer(*buf);
70  delete buf;
71  buf = dup;
72  ASSERT_EQ(0, buf->length());
73 
74  for (i = 0; i <= dataSize; i++)
75  buf->add("b", 1);
76  ASSERT_EQ(dataSize + 1, buf->length());
77 
78  ASSERT_EQ(dataSize, buf->extract(data, dataSize));
79  ASSERT_EQ(1, buf->length());
80 
81  ASSERT_EQ(1, buf->remove(1));
82  ASSERT_EQ(0, buf->length());
83 
84  ASSERT_EQ(0, buf->remove(1));
85 
86  ASSERT_EQ(0, buf->getAt(1, data, dataSize));
87 
88  for (i = 0; i <= dataSize; i++)
89  buf->add("b", 1);
90  ASSERT_EQ(dataSize + 1, buf->length());
91 
92  ASSERT_EQ(dataSize, buf->getAt(1, data, dataSize));
93  ASSERT_EQ(dataSize + 1, buf->length());
94 }
95 
96 TEST_F(DBufferTest, testDBufferBigData)
97 {
98  int count = 100;
99  byte* data;
100  /* this entity string may not contain any inline models, we don't have the bsp tree loaded here */
101  const int size = FS_LoadFile("game/entity.txt", &data);
102  dbuffer buf;
103 
104  for (int i = 0; i < count; i++) {
105  buf.add((char*)data, size);
106  }
107 
108  ASSERT_EQ(size * count, buf.length());
109  FS_FreeFile(data);
110 }
111 
112 TEST_F(DBufferTest, testDBufferNetHandling)
113 {
114  dbuffer buf;
115  NET_WriteByte(&buf, 'b');
116  ASSERT_EQ(1, buf.length());
117  NET_WriteShort(&buf, 128);
118  ASSERT_EQ(3, buf.length());
119  NET_WriteLong(&buf, 128);
120  ASSERT_EQ(7, buf.length());
121 }
int FS_LoadFile(const char *path, byte **buffer)
Filenames are relative to the quake search path.
Definition: files.cpp:384
voidpf void * buf
Definition: ioapi.h:42
void add(const char *, size_t)
Definition: dbuffer.cpp:42
GLsizei size
Definition: r_gl.h:152
void TEST_Shutdown(void)
Definition: test_shared.cpp:34
size_t extract(char *, size_t)
Read and delete data from a dbuffer.
Definition: dbuffer.cpp:136
void NET_WriteLong(dbuffer *buf, int c)
Definition: netpack.cpp:52
TEST_F(DBufferTest, testDBuffer)
QGL_EXTERN GLuint count
Definition: r_gl.h:99
size_t get(char *, size_t) const
Read data from a dbuffer.
Definition: dbuffer.cpp:61
QGL_EXTERN GLint i
Definition: r_gl.h:113
static void TearDownTestCase()
void TEST_Init(void)
Definition: test_shared.cpp:72
void NET_WriteByte(dbuffer *buf, byte c)
Definition: netpack.cpp:39
size_t length() const
Definition: dbuffer.h:48
GLsizei const GLvoid * data
Definition: r_gl.h:152
static void SetUpTestCase()
uint8_t byte
Definition: ufotypes.h:34
void NET_WriteShort(dbuffer *buf, int c)
Definition: netpack.cpp:45
void FS_FreeFile(void *buffer)
Definition: files.cpp:411