Electroneum
encodingstest.cpp File Reference
Include dependency graph for encodingstest.cpp:

Go to the source code of this file.

Macros

#define UTF8_ACCEPT   0u
 

Functions

 TEST (EncodingsTest, UTF8)
 
 TEST (EncodingsTest, UTF16)
 
 TEST (EncodingsTest, UTF32)
 
 TEST (EncodingsTest, ASCII)
 

Macro Definition Documentation

◆ UTF8_ACCEPT

#define UTF8_ACCEPT   0u

Definition at line 242 of file encodingstest.cpp.

Function Documentation

◆ TEST() [1/4]

TEST ( EncodingsTest  ,
UTF8   
)

Definition at line 285 of file encodingstest.cpp.

285  {
286  StringBuffer os, os2;
287  for (const unsigned* range = kCodepointRanges; *range != 0xFFFFFFFF; range += 2) {
288  for (unsigned codepoint = range[0]; codepoint <= range[1]; ++codepoint) {
289  os.Clear();
290  UTF8<>::Encode(os, codepoint);
291  const char* encodedStr = os.GetString();
292 
293  // Decode with Hoehrmann
294  {
295  unsigned decodedCodepoint = 0;
296  unsigned state = 0;
297 
298  unsigned decodedCount = 0;
299  for (const char* s = encodedStr; *s; ++s)
300  if (!decode(&state, &decodedCodepoint, static_cast<unsigned char>(*s))) {
301  EXPECT_EQ(codepoint, decodedCodepoint);
302  decodedCount++;
303  }
304 
305  if (*encodedStr) { // This decoder cannot handle U+0000
306  EXPECT_EQ(1u, decodedCount); // Should only contain one code point
307  }
308 
310  if (UTF8_ACCEPT != state)
311  std::cout << std::hex << codepoint << " " << decodedCodepoint << std::endl;
312  }
313 
314  // Decode
315  {
316  StringStream is(encodedStr);
317  unsigned decodedCodepoint;
318  bool result = UTF8<>::Decode(is, &decodedCodepoint);
319  EXPECT_TRUE(result);
320  EXPECT_EQ(codepoint, decodedCodepoint);
321  if (!result || codepoint != decodedCodepoint)
322  std::cout << std::hex << codepoint << " " << decodedCodepoint << std::endl;
323  }
324 
325  // Validate
326  {
327  StringStream is(encodedStr);
328  os2.Clear();
329  bool result = UTF8<>::Validate(is, os2);
330  EXPECT_TRUE(result);
331  EXPECT_EQ(0, StrCmp(encodedStr, os2.GetString()));
332  }
333  }
334  }
335 }
static bool Decode(InputStream &is, unsigned *codepoint)
Definition: encodings.h:146
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define UTF8_ACCEPT
static bool Validate(InputStream &is, OutputStream &os)
Definition: encodings.h:179
static void Encode(OutputStream &os, unsigned codepoint)
Definition: encodings.h:102
Read-only string stream.
Definition: fwd.h:47
int StrCmp(const Ch *s1, const Ch *s2)
Definition: unittest.h:67
Definition: blake256.h:37
const Ch * GetString() const
Definition: stringbuffer.h:73
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922

◆ TEST() [2/4]

TEST ( EncodingsTest  ,
UTF16   
)

Definition at line 337 of file encodingstest.cpp.

337  {
338  GenericStringBuffer<UTF16<> > os, os2;
340  for (const unsigned* range = kCodepointRanges; *range != 0xFFFFFFFF; range += 2) {
341  for (unsigned codepoint = range[0]; codepoint <= range[1]; ++codepoint) {
342  os.Clear();
343  UTF16<>::Encode(os, codepoint);
344  const UTF16<>::Ch* encodedStr = os.GetString();
345 
346  // Encode with Hoehrmann's code
347  if (codepoint != 0) // cannot handle U+0000
348  {
349  // encode with UTF8<> first
350  utf8os.Clear();
351  UTF8<>::Encode(utf8os, codepoint);
352 
353  // transcode from UTF8 to UTF16 with Hoehrmann's code
354  unsigned decodedCodepoint = 0;
355  unsigned state = 0;
356  UTF16<>::Ch buffer[3], *p = &buffer[0];
357  for (const char* s = utf8os.GetString(); *s; ++s) {
358  if (!decode(&state, &decodedCodepoint, static_cast<unsigned char>(*s)))
359  break;
360  }
361 
362  if (codepoint <= 0xFFFF)
363  *p++ = static_cast<UTF16<>::Ch>(decodedCodepoint);
364  else {
365  // Encode code points above U+FFFF as surrogate pair.
366  *p++ = static_cast<UTF16<>::Ch>(0xD7C0 + (decodedCodepoint >> 10));
367  *p++ = static_cast<UTF16<>::Ch>(0xDC00 + (decodedCodepoint & 0x3FF));
368  }
369  *p++ = '\0';
370 
371  EXPECT_EQ(0, StrCmp(buffer, encodedStr));
372  }
373 
374  // Decode
375  {
376  GenericStringStream<UTF16<> > is(encodedStr);
377  unsigned decodedCodepoint;
378  bool result = UTF16<>::Decode(is, &decodedCodepoint);
379  EXPECT_TRUE(result);
380  EXPECT_EQ(codepoint, decodedCodepoint);
381  if (!result || codepoint != decodedCodepoint)
382  std::cout << std::hex << codepoint << " " << decodedCodepoint << std::endl;
383  }
384 
385  // Validate
386  {
387  GenericStringStream<UTF16<> > is(encodedStr);
388  os2.Clear();
389  bool result = UTF16<>::Validate(is, os2);
390  EXPECT_TRUE(result);
391  EXPECT_EQ(0, StrCmp(encodedStr, os2.GetString()));
392  }
393  }
394  }
395 }
static void Encode(OutputStream &os, unsigned codepoint)
Definition: encodings.h:276
Represents an in-memory output stream.
Definition: fwd.h:59
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
static bool Validate(InputStream &is, OutputStream &os)
Definition: encodings.h:325
static void Encode(OutputStream &os, unsigned codepoint)
Definition: encodings.h:102
Read-only string stream.
Definition: fwd.h:47
int StrCmp(const Ch *s1, const Ch *s2)
Definition: unittest.h:67
Definition: blake256.h:37
CharType Ch
Definition: encodings.h:270
const Ch * GetString() const
Definition: stringbuffer.h:73
static bool Decode(InputStream &is, unsigned *codepoint)
Definition: encodings.h:307
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922

◆ TEST() [3/4]

TEST ( EncodingsTest  ,
UTF32   
)

Definition at line 397 of file encodingstest.cpp.

397  {
398  GenericStringBuffer<UTF32<> > os, os2;
399  for (const unsigned* range = kCodepointRanges; *range != 0xFFFFFFFF; range += 2) {
400  for (unsigned codepoint = range[0]; codepoint <= range[1]; ++codepoint) {
401  os.Clear();
402  UTF32<>::Encode(os, codepoint);
403  const UTF32<>::Ch* encodedStr = os.GetString();
404 
405  // Decode
406  {
407  GenericStringStream<UTF32<> > is(encodedStr);
408  unsigned decodedCodepoint;
409  bool result = UTF32<>::Decode(is, &decodedCodepoint);
410  EXPECT_TRUE(result);
411  EXPECT_EQ(codepoint, decodedCodepoint);
412  if (!result || codepoint != decodedCodepoint)
413  std::cout << std::hex << codepoint << " " << decodedCodepoint << std::endl;
414  }
415 
416  // Validate
417  {
418  GenericStringStream<UTF32<> > is(encodedStr);
419  os2.Clear();
420  bool result = UTF32<>::Validate(is, os2);
421  EXPECT_TRUE(result);
422  EXPECT_EQ(0, StrCmp(encodedStr, os2.GetString()));
423  }
424  }
425  }
426 }
Represents an in-memory output stream.
Definition: fwd.h:59
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
static bool Validate(InputStream &is, OutputStream &os)
Definition: encodings.h:447
static void Encode(OutputStream &os, unsigned codepoint)
Definition: encodings.h:425
Read-only string stream.
Definition: fwd.h:47
int StrCmp(const Ch *s1, const Ch *s2)
Definition: unittest.h:67
const Ch * GetString() const
Definition: stringbuffer.h:73
CharType Ch
Definition: encodings.h:419
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static bool Decode(InputStream &is, unsigned *codepoint)
Definition: encodings.h:439

◆ TEST() [4/4]

TEST ( EncodingsTest  ,
ASCII   
)

Definition at line 428 of file encodingstest.cpp.

428  {
429  StringBuffer os, os2;
430  for (unsigned codepoint = 0; codepoint < 128; codepoint++) {
431  os.Clear();
432  ASCII<>::Encode(os, codepoint);
433  const ASCII<>::Ch* encodedStr = os.GetString();
434  {
435  StringStream is(encodedStr);
436  unsigned decodedCodepoint;
437  bool result = ASCII<>::Decode(is, &decodedCodepoint);
438  if (!result || codepoint != decodedCodepoint)
439  std::cout << std::hex << codepoint << " " << decodedCodepoint << std::endl;
440  }
441 
442  // Validate
443  {
444  StringStream is(encodedStr);
445  os2.Clear();
446  bool result = ASCII<>::Validate(is, os2);
447  EXPECT_TRUE(result);
448  EXPECT_EQ(0, StrCmp(encodedStr, os2.GetString()));
449  }
450  }
451 }
static void Encode(OutputStream &os, unsigned codepoint)
Definition: encodings.h:548
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
Read-only string stream.
Definition: fwd.h:47
CharType Ch
Definition: encodings.h:543
int StrCmp(const Ch *s1, const Ch *s2)
Definition: unittest.h:67
static bool Validate(InputStream &is, OutputStream &os)
Definition: encodings.h:567
const Ch * GetString() const
Definition: stringbuffer.h:73
static bool Decode(InputStream &is, unsigned *codepoint)
Definition: encodings.h:560
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Here is the call graph for this function: