summaryrefslogtreecommitdiffstats
path: root/gfx/sfntly/cpp/src/test/endian_test.cc
blob: 80cce6879af56dd8028a212c49c027d999cdf8a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "gtest/gtest.h"
#include "sfntly/tag.h"
#include "sfntly/data/growable_memory_byte_array.h"
#include "sfntly/data/writable_font_data.h"
#include "sfntly/math/fixed1616.h"
#include "sfntly/port/memory_output_stream.h"
#include "sfntly/data/font_output_stream.h"

namespace sfntly {

bool TestEndian() {
  uint8_t test_data[] = {
      0x68, 0x65, 0x61, 0x64,  // 0: head
      0xca, 0xca, 0xca, 0xca,  // 4: ubyte, byte, char
      0x00, 0x18, 0x80, 0x18,  // 8: ushort, short
      0x00, 0x00, 0x18, 0x00,  // 12: uint24
      0x00, 0x00, 0x00, 0x18,  // 16: ulong
      0xff, 0xff, 0xff, 0x00,  // 20: long
      0x00, 0x01, 0x00, 0x00   // 24: fixed
  };

  ByteArrayPtr ba1 = new GrowableMemoryByteArray();
  for (size_t i = 0; i < sizeof(test_data); ++i) {
    ba1->Put(i, test_data[i]);
  }
  ReadableFontDataPtr rfd = new ReadableFontData(ba1);
  EXPECT_EQ(rfd->ReadULongAsInt(0), Tag::head);
  EXPECT_EQ(rfd->ReadUByte(4), 202);
  EXPECT_EQ(rfd->ReadByte(5), -54);
  EXPECT_EQ(rfd->ReadChar(6), 202);
  EXPECT_EQ(rfd->ReadUShort(8), 24);
  EXPECT_EQ(rfd->ReadShort(10), -32744);
  EXPECT_EQ(rfd->ReadUInt24(12), 24);
  EXPECT_EQ(rfd->ReadULong(16), 24);
  EXPECT_EQ(rfd->ReadLong(20), -256);
  EXPECT_EQ(rfd->ReadFixed(24), Fixed1616::Fixed(1, 0));

  MemoryOutputStream os;
  FontOutputStream fos(&os);
  fos.WriteULong(Tag::head);
  fos.Write(202);
  fos.Write(202);
  fos.Write(202);
  fos.Write(202);
  fos.WriteUShort(24);
  fos.WriteShort(-32744);
  fos.WriteUInt24(24);
  fos.WriteChar(0);
  fos.WriteULong(24);
  fos.WriteLong(-256);
  fos.WriteFixed(Fixed1616::Fixed(1, 0));
  EXPECT_EQ(memcmp(os.Get(), test_data, sizeof(test_data)), 0);

  return true;
}

}  // namespace sfntly

TEST(Endian, All) {
  ASSERT_TRUE(sfntly::TestEndian());
}