blob: 82f394a9dd3746b9b3ae3b516595b2f2cb662919 [file] [log] [blame]
Will Drewry8f367fc2017-03-30 22:07:48 -05001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdint.h>
18
19#include <ese/ese_sg.h>
20#include <gtest/gtest.h>
21
22using ::testing::Test;
23
24class ScatterGatherTest : public virtual Test {
25 public:
26 ScatterGatherTest() {
27 }
28 virtual ~ScatterGatherTest() { }
29 virtual void SetUp() { }
30 virtual void TearDown() { }
31};
32
33TEST_F(ScatterGatherTest, OneToBuf) {
34 uint8_t sg_data[] = "HELLO WORLD";
35 struct EseSgBuffer sg = {
36 .base = &sg_data[0],
37 .len = sizeof(sg_data),
38 };
39 uint8_t dst[sizeof(sg_data) * 2];
40 uint32_t copied;
41 copied = ese_sg_to_buf(&sg, 1, 0, sizeof(dst), dst);
42 EXPECT_EQ(copied, sizeof(sg_data));
43 EXPECT_STREQ(reinterpret_cast<char *>(dst),
44 reinterpret_cast<char *>(sg_data));
45}
46
47TEST_F(ScatterGatherTest, OneFromBuf) {
48 uint8_t src[] = "HELLO WORLD!";
49 uint8_t sg_data[sizeof(src) * 2];
50 struct EseSgBuffer sg = {
51 .base = &sg_data[0],
52 .len = sizeof(sg_data),
53 };
54 uint32_t copied;
55 copied = ese_sg_from_buf(&sg, 1, 0, sizeof(src), src);
56 EXPECT_EQ(copied, sizeof(src));
57 EXPECT_STREQ(reinterpret_cast<char *>(src),
58 reinterpret_cast<char *>(sg_data));
59}
60
61
62TEST_F(ScatterGatherTest, ThreeToBuf) {
63 uint8_t one[] = {'H', 'E', 'L'};
64 uint8_t two[] = {'L', 'O', ' '};
65 uint8_t three[] = "WORLD";
66 struct EseSgBuffer sg[] = {
67 {
68 .base = one,
69 .len = sizeof(one),
70 },
71 {
72 .base = two,
73 .len = sizeof(two),
74 },
75 {
76 .base = three,
77 .len = sizeof(three),
78 },
79 };
80 uint8_t dst[256];
81 uint32_t copied;
82 copied = ese_sg_to_buf(sg, 3, 0, sizeof(dst), dst);
83 EXPECT_EQ(copied, strlen("HELLO WORLD") + 1);
84 EXPECT_STREQ(reinterpret_cast<char *>(dst),
85 "HELLO WORLD");
86 // Again but offset by the first bufs.
87 copied = ese_sg_to_buf(sg, 3, sizeof(one) + sizeof(two) - 2, sizeof(dst),
88 dst);
89 EXPECT_EQ(copied, (strlen("HELLO WORLD") + 1) -
90 (sizeof(one) + sizeof(two) - 2));
91 EXPECT_STREQ(reinterpret_cast<char *>(dst),
92 "O WORLD");
93}
94
95
96TEST_F(ScatterGatherTest, ThreeFromBuf) {
97 uint8_t one[3];
98 uint8_t two[3];
99 uint8_t three[6];
100 struct EseSgBuffer sg[] = {
101 {
102 .base = one,
103 .len = sizeof(one),
104 },
105 {
106 .base = two,
107 .len = sizeof(two),
108 },
109 {
110 .base = three,
111 .len = sizeof(three),
112 },
113 };
114 uint8_t src[] = "HELLO WORLD";
115 uint32_t copied;
116 copied = ese_sg_from_buf(sg, 3, 0, sizeof(src), src);
117 EXPECT_EQ(copied, sizeof(one) + sizeof(two) + sizeof(three));
118 EXPECT_EQ(one[0], 'H');
119 EXPECT_EQ(one[1], 'E');
120 EXPECT_EQ(one[2], 'L');
121 EXPECT_STREQ(reinterpret_cast<char *>(three), "WORLD");
122 // Again but offset.
123 copied = ese_sg_from_buf(sg, 3, 6, sizeof(src), src);
124 EXPECT_EQ(copied, ese_sg_length(sg, 3) - 6);
125 EXPECT_EQ(three[5], ' ');
126 three[5] = 0; // NUL terminate for the test below.
127 EXPECT_STREQ(reinterpret_cast<char *>(three), "HELLO");
128}
129