2 * Copyright 2008, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "native_client/service_runtime/gio.h"
34 #include "third_party/gtest/files/include/gtest/gtest.h"
38 TEST(GioMemTest
, ReadTest
) {
39 struct GioMemoryFile mf
;
46 in_buffer
= (char*) malloc(in_size
);
47 for (int i
= 0; i
< in_size
; ++i
)
50 GioMemoryFileCtor(&mf
, in_buffer
, in_size
);
52 out_buffer
= (char*) malloc(out_size
);
54 // mf_curpos = 0, 32 left, read 16
55 ret_code
= GioMemoryFileRead(&mf
.base
, out_buffer
, 16);
56 EXPECT_EQ(16, ret_code
);
57 for (int i
= 0; i
< 16; ++i
)
58 EXPECT_EQ(i
+'A', out_buffer
[i
]);
60 // mf_curpos = 16, 16 left, read 10
61 ret_code
= GioMemoryFileRead(&mf
.base
, out_buffer
, 10);
62 EXPECT_EQ(10, ret_code
);
63 for (int i
= 0; i
< 10; ++i
)
64 EXPECT_EQ(16+i
+'A', out_buffer
[i
]);
65 // residual value after idx 10
66 EXPECT_EQ(10+'A', out_buffer
[10]);
68 // mf_curpos = 26, 6 left, read 8
69 ret_code
= GioMemoryFileRead(&mf
.base
, out_buffer
, 8);
70 EXPECT_EQ(6, ret_code
);
71 for (int i
= 0; i
< 6; ++i
)
72 EXPECT_EQ(26+i
+'A', out_buffer
[i
]);
73 // residual value after idx 6
74 EXPECT_EQ(22+'A', out_buffer
[6]);
76 // mf_curpos = 32, 0 left, read 16
77 ret_code
= GioMemoryFileRead(&mf
.base
, out_buffer
, 16);
78 EXPECT_EQ(0, ret_code
);
81 TEST(GioMemTest
, WriteTest
) {
82 struct GioMemoryFile mf
;
90 mf_buffer
= (char *) malloc(mf_size
);
91 EXPECT_NE((void*)NULL
, mf_buffer
);
93 GioMemoryFileCtor(&mf
, mf_buffer
, mf_size
);
95 in_buffer
= (char*) malloc(in_size
);
96 for (int i
= 0; i
< in_size
; ++i
)
99 // mf_curpos = 0, 64 left, write 32
100 ret_code
= GioMemoryFileWrite(&mf
.base
, in_buffer
, 32);
101 EXPECT_EQ(32, ret_code
);
103 GioMemoryFileSeek(&mf
.base
, -1, SEEK_CUR
);
104 GioMemoryFileRead(&mf
.base
, &out_char
, 1);
105 EXPECT_EQ(31, out_char
);
107 // mf_curpos = 32, 32 left, write 20
108 ret_code
= GioMemoryFileWrite(&mf
.base
, in_buffer
, 20);
109 EXPECT_EQ(20, ret_code
);
111 GioMemoryFileSeek(&mf
.base
, -1, SEEK_CUR
);
112 GioMemoryFileRead(&mf
.base
, &out_char
, 1);
113 EXPECT_EQ(19, out_char
);
115 // mf_curpos = 52, 12 left, write 20
116 ret_code
= GioMemoryFileWrite(&mf
.base
, in_buffer
, 20);
117 EXPECT_EQ(12, ret_code
);
119 GioMemoryFileSeek(&mf
.base
, -1, SEEK_CUR
);
120 GioMemoryFileRead(&mf
.base
, &out_char
, 1);
121 EXPECT_EQ(11, out_char
);
123 // mf_curpos = 64, 0 left, write 20
124 ret_code
= GioMemoryFileWrite(&mf
.base
, in_buffer
, 20);
125 EXPECT_EQ(0, ret_code
);
128 TEST(GioMemTest
, SeekTest
) {
129 struct GioMemoryFile mf
;
135 in_buffer
= (char*) malloc(in_size
);
136 for (int i
= 0; i
< in_size
; ++i
)
139 GioMemoryFileCtor(&mf
, in_buffer
, in_size
);
142 ret_code
= GioMemoryFileSeek(&mf
.base
, 15, SEEK_SET
);
143 EXPECT_EQ(15, ret_code
);
145 ret_code
= GioMemoryFileRead(&mf
.base
, &out_char
, 1);
146 EXPECT_EQ(1, ret_code
);
147 EXPECT_EQ(15, out_char
);
150 ret_code
= GioMemoryFileSeek(&mf
.base
, 4, SEEK_CUR
);
151 EXPECT_EQ(20, ret_code
);
153 ret_code
= GioMemoryFileRead(&mf
.base
, &out_char
, 1);
154 EXPECT_EQ(1, ret_code
);
155 EXPECT_EQ(20, out_char
);
158 ret_code
= GioMemoryFileSeek(&mf
.base
, -4, SEEK_CUR
);
159 EXPECT_EQ(17, ret_code
);
161 ret_code
= GioMemoryFileRead(&mf
.base
, &out_char
, 1);
162 EXPECT_EQ(1, ret_code
);
163 EXPECT_EQ(17, out_char
);
166 ret_code
= GioMemoryFileSeek(&mf
.base
, -4, SEEK_END
);
167 EXPECT_EQ(28, ret_code
);
169 GioMemoryFileRead(&mf
.base
, &out_char
, 1);
170 EXPECT_EQ(28, out_char
);
173 ret_code
= GioMemoryFileSeek(&mf
.base
, 4, SEEK_END
);
174 EXPECT_EQ(-1, ret_code
);
176 GioMemoryFileRead(&mf
.base
, &out_char
, 1);
177 EXPECT_EQ(29, out_char
);
180 ret_code
= GioMemoryFileSeek(&mf
.base
, 4, SEEK_END
+3);
181 EXPECT_EQ(-1, ret_code
);
183 GioMemoryFileRead(&mf
.base
, &out_char
, 1);
184 EXPECT_EQ(30, out_char
);
187 ret_code
= GioMemoryFileSeek(&mf
.base
, -5, SEEK_SET
);
188 EXPECT_EQ(-1, ret_code
);
190 GioMemoryFileRead(&mf
.base
, &out_char
, 1);
191 EXPECT_EQ(31, out_char
);