Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(741)

Side by Side Diff: tests/DataRefTest.cpp

Issue 15941025: Add SkData::NewFromFD. (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Remove an indentation level. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/utils/win/SkDWriteFontFileStream.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2011 Google Inc. 3 * Copyright 2011 Google Inc.
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 #include "Test.h" 8 #include "Test.h"
9 #include "SkData.h" 9 #include "SkData.h"
10 #include "SkDataSet.h" 10 #include "SkDataSet.h"
11 #include "SkDataTable.h" 11 #include "SkDataTable.h"
12 #include "SkStream.h"
13 #include "SkOrderedReadBuffer.h" 12 #include "SkOrderedReadBuffer.h"
14 #include "SkOrderedWriteBuffer.h" 13 #include "SkOrderedWriteBuffer.h"
15 14 #include "SkOSFile.h"
16 template <typename T> class SkTUnref { 15 #include "SkStream.h"
17 public:
18 SkTUnref(T* ref) : fRef(ref) {}
19 ~SkTUnref() { fRef->unref(); }
20
21 operator T*() { return fRef; }
22 operator const T*() { return fRef; }
23
24 private:
25 T* fRef;
26 };
27 16
28 static void test_is_equal(skiatest::Reporter* reporter, 17 static void test_is_equal(skiatest::Reporter* reporter,
29 const SkDataTable* a, const SkDataTable* b) { 18 const SkDataTable* a, const SkDataTable* b) {
30 REPORTER_ASSERT(reporter, a->count() == b->count()); 19 REPORTER_ASSERT(reporter, a->count() == b->count());
31 for (int i = 0; i < a->count(); ++i) { 20 for (int i = 0; i < a->count(); ++i) {
32 size_t sizea, sizeb; 21 size_t sizea, sizeb;
33 const void* mema = a->at(i, &sizea); 22 const void* mema = a->at(i, &sizea);
34 const void* memb = b->at(i, &sizeb); 23 const void* memb = b->at(i, &sizeb);
35 REPORTER_ASSERT(reporter, sizea == sizeb); 24 REPORTER_ASSERT(reporter, sizea == sizeb);
36 REPORTER_ASSERT(reporter, !memcmp(mema, memb, sizea)); 25 REPORTER_ASSERT(reporter, !memcmp(mema, memb, sizea));
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 ds.writeToStream(&ostream); 205 ds.writeToStream(&ostream);
217 SkMemoryStream istream; 206 SkMemoryStream istream;
218 istream.setData(ostream.copyToData())->unref(); 207 istream.setData(ostream.copyToData())->unref();
219 SkDataSet copy(&istream); 208 SkDataSet copy(&istream);
220 209
221 test_datasets_equal(reporter, ds, copy); 210 test_datasets_equal(reporter, ds, copy);
222 } 211 }
223 212
224 static void test_dataset(skiatest::Reporter* reporter) { 213 static void test_dataset(skiatest::Reporter* reporter) {
225 SkDataSet set0(NULL, 0); 214 SkDataSet set0(NULL, 0);
226 SkDataSet set1("hello", SkTUnref<SkData>(SkData::NewWithCString("world"))); 215 SkDataSet set1("hello", SkAutoTUnref<SkData>(SkData::NewWithCString("world") ));
227 216
228 const SkDataSet::Pair pairs[] = { 217 const SkDataSet::Pair pairs[] = {
229 { "one", SkData::NewWithCString("1") }, 218 { "one", SkData::NewWithCString("1") },
230 { "two", SkData::NewWithCString("2") }, 219 { "two", SkData::NewWithCString("2") },
231 { "three", SkData::NewWithCString("3") }, 220 { "three", SkData::NewWithCString("3") },
232 }; 221 };
233 SkDataSet set3(pairs, 3); 222 SkDataSet set3(pairs, 3);
234 unrefAll(pairs, 3); 223 unrefAll(pairs, 3);
235 224
236 test_dataset(reporter, set0, 0); 225 test_dataset(reporter, set0, 0);
(...skipping 26 matching lines...) Expand all
263 SkAutoTUnref<SkData> r0(SkData::NewWithCopy(str, len + 1)); 252 SkAutoTUnref<SkData> r0(SkData::NewWithCopy(str, len + 1));
264 SkAutoTUnref<SkData> r1(SkData::NewWithCString(str)); 253 SkAutoTUnref<SkData> r1(SkData::NewWithCString(str));
265 254
266 REPORTER_ASSERT(reporter, r0->equals(r1)); 255 REPORTER_ASSERT(reporter, r0->equals(r1));
267 256
268 SkAutoTUnref<SkData> r2(SkData::NewWithCString(NULL)); 257 SkAutoTUnref<SkData> r2(SkData::NewWithCString(NULL));
269 REPORTER_ASSERT(reporter, 1 == r2->size()); 258 REPORTER_ASSERT(reporter, 1 == r2->size());
270 REPORTER_ASSERT(reporter, 0 == *r2->bytes()); 259 REPORTER_ASSERT(reporter, 0 == *r2->bytes());
271 } 260 }
272 261
262 static void test_files(skiatest::Reporter* reporter) {
263 if (skiatest::Test::GetTmpDir().isEmpty()) {
264 return;
265 }
266
267 const char* tmpDir = skiatest::Test::GetTmpDir().c_str();
268 SkString path;
269 path.printf("%s%s", tmpDir, "data_test");
270
271 const char s[] = "abcdefghijklmnopqrstuvwxyz";
272 {
273 SkFILEWStream writer(path.c_str());
274 if (!writer.isValid()) {
275 SkString msg;
276 msg.printf("Failed to create tmp file %s\n", path.c_str());
277 reporter->reportFailed(msg.c_str());
278 return;
279 }
280 writer.write(s, 26);
281 }
282
283 SkFILE* file = sk_fopen(path.c_str(), kRead_SkFILE_Flag);
284 SkAutoTUnref<SkData> r1(SkData::NewFromFILE(file));
285 REPORTER_ASSERT(reporter, r1.get() != NULL);
286 REPORTER_ASSERT(reporter, r1->size() == 26);
287 REPORTER_ASSERT(reporter, strncmp(static_cast<const char*>(r1->data()), s, 2 6) == 0);
288
289 int fd = sk_fileno(file);
290 SkAutoTUnref<SkData> r2(SkData::NewFromFD(fd));
291 REPORTER_ASSERT(reporter, r2.get() != NULL);
292 REPORTER_ASSERT(reporter, r2->size() == 26);
293 REPORTER_ASSERT(reporter, strncmp(static_cast<const char*>(r2->data()), s, 2 6) == 0);
294 }
295
273 static void TestData(skiatest::Reporter* reporter) { 296 static void TestData(skiatest::Reporter* reporter) {
274 const char* str = "We the people, in order to form a more perfect union."; 297 const char* str = "We the people, in order to form a more perfect union.";
275 const int N = 10; 298 const int N = 10;
276 299
277 SkAutoTUnref<SkData> r0(SkData::NewEmpty()); 300 SkAutoTUnref<SkData> r0(SkData::NewEmpty());
278 SkAutoTUnref<SkData> r1(SkData::NewWithCopy(str, strlen(str))); 301 SkAutoTUnref<SkData> r1(SkData::NewWithCopy(str, strlen(str)));
279 SkAutoTUnref<SkData> r2(SkData::NewWithProc(new int[N], N*sizeof(int), 302 SkAutoTUnref<SkData> r2(SkData::NewWithProc(new int[N], N*sizeof(int),
280 delete_int_proc, gGlobal)); 303 delete_int_proc, gGlobal));
281 SkAutoTUnref<SkData> r3(SkData::NewSubset(r1, 7, 6)); 304 SkAutoTUnref<SkData> r3(SkData::NewSubset(r1, 7, 6));
282 305
283 assert_len(reporter, r0, 0); 306 assert_len(reporter, r0, 0);
284 assert_len(reporter, r1, strlen(str)); 307 assert_len(reporter, r1, strlen(str));
285 assert_len(reporter, r2, N * sizeof(int)); 308 assert_len(reporter, r2, N * sizeof(int));
286 assert_len(reporter, r3, 6); 309 assert_len(reporter, r3, 6);
287 310
288 assert_data(reporter, r1, str, strlen(str)); 311 assert_data(reporter, r1, str, strlen(str));
289 assert_data(reporter, r3, "people", 6); 312 assert_data(reporter, r3, "people", 6);
290 313
291 SkData* tmp = SkData::NewSubset(r1, strlen(str), 10); 314 SkData* tmp = SkData::NewSubset(r1, strlen(str), 10);
292 assert_len(reporter, tmp, 0); 315 assert_len(reporter, tmp, 0);
293 tmp->unref(); 316 tmp->unref();
294 tmp = SkData::NewSubset(r1, 0, 0); 317 tmp = SkData::NewSubset(r1, 0, 0);
295 assert_len(reporter, tmp, 0); 318 assert_len(reporter, tmp, 0);
296 tmp->unref(); 319 tmp->unref();
297 320
298 test_cstring(reporter); 321 test_cstring(reporter);
299 test_dataset(reporter); 322 test_dataset(reporter);
323 test_files(reporter);
300 } 324 }
301 325
302 #include "TestClassDef.h" 326 #include "TestClassDef.h"
303 DEFINE_TESTCLASS("Data", DataTestClass, TestData) 327 DEFINE_TESTCLASS("Data", DataTestClass, TestData)
304 DEFINE_TESTCLASS("DataTable", DataTableTestClass, TestDataTable) 328 DEFINE_TESTCLASS("DataTable", DataTableTestClass, TestDataTable)
OLDNEW
« no previous file with comments | « src/utils/win/SkDWriteFontFileStream.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698