Flutter Engine
message_unittests.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "flutter/fml/message.h"
6 
7 #include "gtest/gtest.h"
8 
9 namespace fml {
10 
11 struct TestStruct {
12  int a = 12;
13  char b = 'x';
14  float c = 99.0f;
15 };
16 
17 TEST(MessageTest, CanEncodeTriviallyCopyableTypes) {
18  Message message;
19  ASSERT_TRUE(message.Encode(12));
20  ASSERT_TRUE(message.Encode(11.0f));
21  ASSERT_TRUE(message.Encode('a'));
22 
23  TestStruct s;
24  ASSERT_TRUE(message.Encode(s));
25  ASSERT_GE(message.GetDataLength(), 0u);
26  ASSERT_GE(message.GetBufferSize(), 0u);
27  ASSERT_EQ(message.GetSizeRead(), 0u);
28 }
29 
30 TEST(MessageTest, CanDecodeTriviallyCopyableTypes) {
31  Message message;
32  ASSERT_TRUE(message.Encode(12));
33  ASSERT_TRUE(message.Encode(11.0f));
34  ASSERT_TRUE(message.Encode('a'));
35  TestStruct s;
36  s.a = 10;
37  s.b = 'y';
38  s.c = 11.1f;
39 
40  ASSERT_TRUE(message.Encode(s));
41 
42  ASSERT_GE(message.GetDataLength(), 0u);
43  ASSERT_GE(message.GetBufferSize(), 0u);
44  ASSERT_EQ(message.GetSizeRead(), 0u);
45 
46  int int1 = 0;
47  ASSERT_TRUE(message.Decode(int1));
48  ASSERT_EQ(12, int1);
49 
50  float float1 = 0.0f;
51  ASSERT_TRUE(message.Decode(float1));
52  ASSERT_EQ(float1, 11.0f);
53 
54  char char1 = 'x';
55  ASSERT_TRUE(message.Decode(char1));
56  ASSERT_EQ(char1, 'a');
57 
58  TestStruct s1;
59  ASSERT_TRUE(message.Decode(s1));
60  ASSERT_EQ(s1.a, 10);
61  ASSERT_EQ(s1.b, 'y');
62  ASSERT_EQ(s1.c, 11.1f);
63 
64  ASSERT_NE(message.GetSizeRead(), 0u);
65  ASSERT_EQ(message.GetDataLength(), message.GetSizeRead());
66 }
67 
68 } // namespace fml
bool Decode(T &value)
Definition: message.h:133
Definition: ascii_trie.cc:9
size_t GetSizeRead() const
Definition: message.cc:47
size_t GetBufferSize() const
Definition: message.cc:39
TEST(MessageTest, CanEncodeTriviallyCopyableTypes)
bool Encode(const T &value)
Definition: message.h:90
size_t GetDataLength() const
Definition: message.cc:43