Electroneum
EncodedStreamTest Class Reference
Inheritance diagram for EncodedStreamTest:
Collaboration diagram for EncodedStreamTest:

Public Member Functions

 EncodedStreamTest ()
 
virtual ~EncodedStreamTest ()
 
virtual void SetUp ()
 
virtual void TearDown ()
 
- Public Member Functions inherited from testing::Test
virtual ~Test ()
 
virtual ~Test ()
 

Protected Member Functions

template<typename FileEncoding , typename MemoryEncoding >
void TestEncodedInputStream (const char *filename)
 
void TestAutoUTFInputStream (const char *filename, bool expectHasBOM)
 
template<typename FileEncoding , typename MemoryEncoding >
void TestEncodedOutputStream (const char *expectedFilename, bool putBOM)
 
void TestAutoUTFOutputStream (UTFType type, bool putBOM, const char *expectedFilename)
 
bool CompareFile (const char *filename, const char *expectedFilename)
 
bool CompareBufferFile (const char *actualBuffer, size_t actualLength, const char *expectedFilename)
 
- Protected Member Functions inherited from testing::Test
 Test ()
 
 Test ()
 

Static Protected Member Functions

static FILE * Open (const char *filename)
 
static char * ReadFile (const char *filename, bool appendPath, size_t *outLength)
 

Protected Attributes

char * json_
 
size_t length_
 

Additional Inherited Members

- Public Types inherited from testing::Test
typedef internal::SetUpTestCaseFunc SetUpTestCaseFunc
 
typedef internal::TearDownTestCaseFunc TearDownTestCaseFunc
 
typedef internal::SetUpTestCaseFunc SetUpTestCaseFunc
 
typedef internal::TearDownTestCaseFunc TearDownTestCaseFunc
 
- Static Public Member Functions inherited from testing::Test
static void SetUpTestCase ()
 
static void TearDownTestCase ()
 
static bool HasFatalFailure ()
 
static bool HasNonfatalFailure ()
 
static bool HasFailure ()
 
static void RecordProperty (const std::string &key, const std::string &value)
 
static void RecordProperty (const std::string &key, int value)
 
static void SetUpTestCase ()
 
static void TearDownTestCase ()
 
static bool HasFatalFailure ()
 
static bool HasNonfatalFailure ()
 
static bool HasFailure ()
 
static void RecordProperty (const std::string &key, const std::string &value)
 
static void RecordProperty (const std::string &key, int value)
 

Detailed Description

Definition at line 25 of file encodedstreamtest.cpp.

Constructor & Destructor Documentation

◆ EncodedStreamTest()

EncodedStreamTest::EncodedStreamTest ( )
inline

Definition at line 27 of file encodedstreamtest.cpp.

◆ ~EncodedStreamTest()

EncodedStreamTest::~EncodedStreamTest ( )
virtual

Definition at line 252 of file encodedstreamtest.cpp.

252 {}

Member Function Documentation

◆ CompareBufferFile()

bool EncodedStreamTest::CompareBufferFile ( const char *  actualBuffer,
size_t  actualLength,
const char *  expectedFilename 
)
inlineprotected

Definition at line 240 of file encodedstreamtest.cpp.

240  {
241  size_t expectedLength;
242  char* expectedBuffer = ReadFile(expectedFilename, true, &expectedLength);
243  bool ret = (expectedLength == actualLength) && memcmp(expectedBuffer, actualBuffer, actualLength) == 0;
244  free(expectedBuffer);
245  return ret;
246  }
static char * ReadFile(const char *filename, bool appendPath, size_t *outLength)

◆ CompareFile()

bool EncodedStreamTest::CompareFile ( const char *  filename,
const char *  expectedFilename 
)
inlineprotected

Definition at line 230 of file encodedstreamtest.cpp.

230  {
231  size_t actualLength, expectedLength;
232  char* actualBuffer = ReadFile(filename, false, &actualLength);
233  char* expectedBuffer = ReadFile(expectedFilename, true, &expectedLength);
234  bool ret = (expectedLength == actualLength) && memcmp(expectedBuffer, actualBuffer, actualLength) == 0;
235  free(actualBuffer);
236  free(expectedBuffer);
237  return ret;
238  }
static char * ReadFile(const char *filename, bool appendPath, size_t *outLength)

◆ Open()

static FILE* EncodedStreamTest::Open ( const char *  filename)
inlinestaticprotected

Definition at line 44 of file encodedstreamtest.cpp.

44  {
45  const char *paths[] = {
46  "encodings",
47  "bin/encodings",
48  "../bin/encodings",
49  "../../bin/encodings",
50  "../../../bin/encodings"
51  };
52  char buffer[1024];
53  for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) {
54  sprintf(buffer, "%s/%s", paths[i], filename);
55  FILE *fp = fopen(buffer, "rb");
56  if (fp)
57  return fp;
58  }
59  return 0;
60  }

◆ ReadFile()

static char* EncodedStreamTest::ReadFile ( const char *  filename,
bool  appendPath,
size_t *  outLength 
)
inlinestaticprotected

Definition at line 62 of file encodedstreamtest.cpp.

62  {
63  FILE *fp = appendPath ? Open(filename) : fopen(filename, "rb");
64 
65  if (!fp) {
66  *outLength = 0;
67  return 0;
68  }
69 
70  fseek(fp, 0, SEEK_END);
71  *outLength = static_cast<size_t>(ftell(fp));
72  fseek(fp, 0, SEEK_SET);
73  char* buffer = static_cast<char*>(malloc(*outLength + 1));
74  size_t readLength = fread(buffer, 1, *outLength, fp);
75  buffer[readLength] = '\0';
76  fclose(fp);
77  return buffer;
78  }
static FILE * Open(const char *filename)

◆ SetUp()

virtual void EncodedStreamTest::SetUp ( )
inlinevirtual

Reimplemented from testing::Test.

Definition at line 30 of file encodedstreamtest.cpp.

30  {
31  json_ = ReadFile("utf8.json", true, &length_);
32  }
static char * ReadFile(const char *filename, bool appendPath, size_t *outLength)

◆ TearDown()

virtual void EncodedStreamTest::TearDown ( )
inlinevirtual

Reimplemented from testing::Test.

Definition at line 34 of file encodedstreamtest.cpp.

34  {
35  free(json_);
36  json_ = 0;
37  }

◆ TestAutoUTFInputStream()

void EncodedStreamTest::TestAutoUTFInputStream ( const char *  filename,
bool  expectHasBOM 
)
inlineprotected

Definition at line 121 of file encodedstreamtest.cpp.

121  {
122  // Test FileReadStream
123  {
124  char buffer[16];
125  FILE *fp = Open(filename);
126  ASSERT_TRUE(fp != 0);
127  FileReadStream fs(fp, buffer, sizeof(buffer));
129  EXPECT_EQ(expectHasBOM, eis.HasBOM());
130  StringStream s(json_);
131  while (eis.Peek() != '\0') {
132  unsigned expected, actual;
133  EXPECT_TRUE(UTF8<>::Decode(s, &expected));
134  EXPECT_TRUE(AutoUTF<unsigned>::Decode(eis, &actual));
135  EXPECT_EQ(expected, actual);
136  }
137  EXPECT_EQ('\0', s.Peek());
138  fclose(fp);
139  }
140 
141  // Test MemoryStream
142  {
143  size_t size;
144  char* data = ReadFile(filename, true, &size);
145  MemoryStream ms(data, size);
147  EXPECT_EQ(expectHasBOM, eis.HasBOM());
148  StringStream s(json_);
149 
150  while (eis.Peek() != '\0') {
151  unsigned expected, actual;
152  EXPECT_TRUE(UTF8<>::Decode(s, &expected));
153  EXPECT_TRUE(AutoUTF<unsigned>::Decode(eis, &actual));
154  EXPECT_EQ(expected, actual);
155  }
156  EXPECT_EQ('\0', s.Peek());
157  free(data);
158  EXPECT_EQ(size, eis.Tell());
159  }
160  }
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
Input stream wrapper with dynamically bound encoding and automatic encoding detection.
Represents an in-memory input byte stream.
Definition: memorystream.h:40
File byte stream for input using fread().
Dynamically select encoding according to stream&#39;s runtime-specified UTF encoding type.
Definition: encodings.h:615
Read-only string stream.
Definition: fwd.h:47
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
static FILE * Open(const char *filename)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
UTF-8 encoding.
Definition: encodings.h:96
static char * ReadFile(const char *filename, bool appendPath, size_t *outLength)

◆ TestAutoUTFOutputStream()

void EncodedStreamTest::TestAutoUTFOutputStream ( UTFType  type,
bool  putBOM,
const char *  expectedFilename 
)
inlineprotected

Definition at line 196 of file encodedstreamtest.cpp.

196  {
197  // Test FileWriteStream
198  {
199  char filename[L_tmpnam];
200  FILE* fp = TempFile(filename);
201 
202  char buffer[16];
203  FileWriteStream os(fp, buffer, sizeof(buffer));
205  StringStream s(json_);
206  while (s.Peek() != '\0') {
207  bool success = Transcoder<UTF8<>, AutoUTF<unsigned> >::Transcode(s, eos);
209  }
210  eos.Flush();
211  fclose(fp);
212  EXPECT_TRUE(CompareFile(filename, expectedFilename));
213  remove(filename);
214  }
215 
216  // Test MemoryBuffer
217  {
218  MemoryBuffer mb;
219  AutoUTFOutputStream<unsigned, MemoryBuffer> eos(mb, type, putBOM);
220  StringStream s(json_);
221  while (s.Peek() != '\0') {
222  bool success = Transcoder<UTF8<>, AutoUTF<unsigned> >::Transcode(s, eos);
224  }
225  eos.Flush();
226  EXPECT_TRUE(CompareBufferFile(mb.GetBuffer(), mb.GetSize(), expectedFilename));
227  }
228  }
size_t GetSize() const
Definition: memorybuffer.h:54
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
FILE * TempFile(char *filename)
Definition: unittest.h:80
Encoding conversion.
Definition: encodings.h:658
bool CompareBufferFile(const char *actualBuffer, size_t actualLength, const char *expectedFilename)
Read-only string stream.
Definition: fwd.h:47
bool CompareFile(const char *filename, const char *expectedFilename)
Represents an in-memory output byte stream.
Definition: fwd.h:74
Output stream wrapper with dynamically bound encoding and automatic encoding detection.
expect< void > success() noexcept
Definition: expect.h:397
const Ch * GetBuffer() const
Definition: memorybuffer.h:50
Wrapper of C file stream for output using fwrite().
Here is the call graph for this function:

◆ TestEncodedInputStream()

template<typename FileEncoding , typename MemoryEncoding >
void EncodedStreamTest::TestEncodedInputStream ( const char *  filename)
inlineprotected

Definition at line 81 of file encodedstreamtest.cpp.

81  {
82  // Test FileReadStream
83  {
84  char buffer[16];
85  FILE *fp = Open(filename);
86  ASSERT_TRUE(fp != 0);
87  FileReadStream fs(fp, buffer, sizeof(buffer));
90 
91  while (eis.Peek() != '\0') {
92  unsigned expected, actual;
93  EXPECT_TRUE(UTF8<>::Decode(s, &expected));
94  EXPECT_TRUE(MemoryEncoding::Decode(eis, &actual));
95  EXPECT_EQ(expected, actual);
96  }
97  EXPECT_EQ('\0', s.Peek());
98  fclose(fp);
99  }
100 
101  // Test MemoryStream
102  {
103  size_t size;
104  char* data = ReadFile(filename, true, &size);
105  MemoryStream ms(data, size);
107  StringStream s(json_);
108 
109  while (eis.Peek() != '\0') {
110  unsigned expected, actual;
111  EXPECT_TRUE(UTF8<>::Decode(s, &expected));
112  EXPECT_TRUE(MemoryEncoding::Decode(eis, &actual));
113  EXPECT_EQ(expected, actual);
114  }
115  EXPECT_EQ('\0', s.Peek());
116  free(data);
117  EXPECT_EQ(size, eis.Tell());
118  }
119  }
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
Represents an in-memory input byte stream.
Definition: memorystream.h:40
File byte stream for input using fread().
Read-only string stream.
Definition: fwd.h:47
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
Input byte stream wrapper with a statically bound encoding.
Definition: encodedstream.h:39
static FILE * Open(const char *filename)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
UTF-8 encoding.
Definition: encodings.h:96
static char * ReadFile(const char *filename, bool appendPath, size_t *outLength)

◆ TestEncodedOutputStream()

template<typename FileEncoding , typename MemoryEncoding >
void EncodedStreamTest::TestEncodedOutputStream ( const char *  expectedFilename,
bool  putBOM 
)
inlineprotected

Definition at line 163 of file encodedstreamtest.cpp.

163  {
164  // Test FileWriteStream
165  {
166  char filename[L_tmpnam];
167  FILE* fp = TempFile(filename);
168  char buffer[16];
169  FileWriteStream os(fp, buffer, sizeof(buffer));
171  StringStream s(json_);
172  while (s.Peek() != '\0') {
173  bool success = Transcoder<UTF8<>, MemoryEncoding>::Transcode(s, eos);
175  }
176  eos.Flush();
177  fclose(fp);
178  EXPECT_TRUE(CompareFile(filename, expectedFilename));
179  remove(filename);
180  }
181 
182  // Test MemoryBuffer
183  {
184  MemoryBuffer mb;
186  StringStream s(json_);
187  while (s.Peek() != '\0') {
188  bool success = Transcoder<UTF8<>, MemoryEncoding>::Transcode(s, eos);
190  }
191  eos.Flush();
192  EXPECT_TRUE(CompareBufferFile(mb.GetBuffer(), mb.GetSize(), expectedFilename));
193  }
194  }
size_t GetSize() const
Definition: memorybuffer.h:54
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
Output byte stream wrapper with statically bound encoding.
FILE * TempFile(char *filename)
Definition: unittest.h:80
Encoding conversion.
Definition: encodings.h:658
bool CompareBufferFile(const char *actualBuffer, size_t actualLength, const char *expectedFilename)
Read-only string stream.
Definition: fwd.h:47
bool CompareFile(const char *filename, const char *expectedFilename)
Represents an in-memory output byte stream.
Definition: fwd.h:74
expect< void > success() noexcept
Definition: expect.h:397
const Ch * GetBuffer() const
Definition: memorybuffer.h:50
Wrapper of C file stream for output using fwrite().
Here is the call graph for this function:

Member Data Documentation

◆ json_

char* EncodedStreamTest::json_
protected

Definition at line 248 of file encodedstreamtest.cpp.

◆ length_

size_t EncodedStreamTest::length_
protected

Definition at line 249 of file encodedstreamtest.cpp.


The documentation for this class was generated from the following file: