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

Side by Side Diff: tests/ValueTest.cpp

Issue 1604253002: SkValue: implementation, unit test (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: stray whitespce remvd Created 4 years, 11 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
« src/core/SkValue.cpp ('K') | « src/core/SkValue.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 * Copyright 2016 Google Inc. 2 * Copyright 2016 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkData.h"
9 #include "SkValue.h"
8 #include "Test.h" 10 #include "Test.h"
9 #include "SkValue.h" 11
12 static const SkValue::Type example_type =
13 SkValue::Type(SkValue::kMaxPublicObject + 1);
14
15 enum { kExampleS32, kExampleF32, kExampleU32, kExampleObject, kExampleBytes,
16 kExampleF32s, kExampleU32s, kExampleU16s, kExampleArray };
17
18 static const uint16_t aU16[] = { 1, 2, 3, 4 };
19 static const uint32_t aU32[] = { 5, 6, 7, 8 };
20 static const float aF32[] = { 9.0f, 9.125f, 9.25f };
21 static const char hello[] = "HELLO";
22
23 static SkValue make_example(skiatest::Reporter* r, int level = 4) {
24 auto value = SkValue::Object(example_type);
25 value.set(kExampleU32, SkValue::FromU32(1000));
26 value.set(kExampleS32, SkValue::FromS32(-123));
27 value.set(kExampleF32, SkValue::FromF32(0.5f));
28 value.set(kExampleU32, SkValue::FromU32(1234));
29 if (level > 0) {
30 value.set(kExampleObject, make_example(r, 0));
31 value.set(kExampleObject, make_example(r, level - 1)); // replace
32 }
33 SkAutoTUnref<SkData> data(SkData::NewWithCString(hello));
34 value.set(kExampleBytes, SkValue::FromBytes(data));
35
36 SkAutoTUnref<SkData> dataU16(SkData::NewWithCopy(aU16, sizeof(aU16)));
37 SkAutoTUnref<SkData> dataU32(SkData::NewWithCopy(aU32, sizeof(aU32)));
38 SkAutoTUnref<SkData> dataF32(SkData::NewWithCopy(aF32, sizeof(aF32)));
39 value.set(kExampleU16s, SkValue::FromU16s(dataU16));
40 value.set(kExampleU32s, SkValue::FromU32s(dataU32));
41 value.set(kExampleF32s, SkValue::FromF32s(dataF32));
42
43 auto varray = SkValue::ValueArray();
44 varray.append(SkValue::FromU32(99));
45 varray.append(SkValue::FromS32(-99));
46 value.set(kExampleArray, std::move(varray));
47 return value;
48 }
49
50 DEF_TEST(Value, r) {
51 SkValue val = make_example(r);
52 REPORTER_ASSERT(r, example_type == val.type());
53 SkValue valCopy = val;
54 REPORTER_ASSERT(r, example_type == valCopy.type());
55 valCopy.set(4321, SkValue());
56 auto fn = [&](SkValue::Key k, const SkValue& v){
57 int count;
58 switch (k) {
59 case kExampleS32:
60 REPORTER_ASSERT(r, -123 == v.s32());
61 break;
62 case kExampleF32:
63 REPORTER_ASSERT(r, 0.5f == v.f32());
64 break;
65 case kExampleU32:
66 REPORTER_ASSERT(r, 1234 == v.u32());
67 break;
68 case kExampleObject:
69 REPORTER_ASSERT(r, example_type == v.type());
70 break;
71 case kExampleBytes:
72 REPORTER_ASSERT(r, v.type() == SkValue::Bytes && v.bytes()
73 && v.bytes()->size() == sizeof(hello));
74 break;
75 case kExampleF32s:
76 REPORTER_ASSERT(r, v.type() == SkValue::F32s && v.bytes()
77 && v.bytes()->size() == sizeof(aF32)
78 && v.f32s(&count)
79 && count == SK_ARRAY_COUNT(aF32));
80 break;
81 case kExampleU32s:
82 REPORTER_ASSERT(r, v.type() == SkValue::U32s && v.bytes()
83 && v.bytes()->size() == sizeof(aU32)
84 && v.u32s(&count)
85 && count == SK_ARRAY_COUNT(aU32));
86 break;
87 case kExampleU16s:
88 REPORTER_ASSERT(r, v.type() == SkValue::U16s && v.bytes()
89 && v.bytes()->size() == sizeof(aU16)
90 && v.u16s(&count)
91 && count == SK_ARRAY_COUNT(aU16));
92 break;
93 case kExampleArray:
94 REPORTER_ASSERT(r, v.type() == SkValue::Array
95 && v.length() == 2);
96 break;
97 default:
98 ERRORF(r, "unexpected key");
99 }
100 };
101 val.foreach(fn);
102 }
OLDNEW
« src/core/SkValue.cpp ('K') | « src/core/SkValue.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698