Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkBase64Test.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
11#include "src/base/SkBase64.h"
12#include "tests/Test.h"
13
14#include <cstring>
15
16using namespace skia_private;
17
19 char all[256];
20 for (int index = 0; index < 255; ++index) {
21 all[index] = (signed char) (index + 1);
22 }
23 all[255] = 0;
24
25 for (int offset = 0; offset < 6; ++offset) {
26 size_t length = 256 - offset;
27
28 // Encode
29 size_t predictedEncodeLength = SkBase64::EncodedSize(length);
30 size_t actualEncodeLength = SkBase64::Encode(all + offset, length, nullptr);
31
32 REPORTER_ASSERT(reporter, actualEncodeLength == predictedEncodeLength,
33 "input size %zu; output size %zu != %zu", length,
34 actualEncodeLength, predictedEncodeLength);
35 AutoTMalloc<char> src(actualEncodeLength + 1);
36 size_t n = SkBase64::Encode(all + offset, length, src.get());
37 REPORTER_ASSERT(reporter, n == predictedEncodeLength);
38
39 src[SkToInt(actualEncodeLength)] = '\0';
40
41 // Decode
43
44 size_t decodeLength;
45 err = SkBase64::Decode(src.get(), actualEncodeLength, nullptr, &decodeLength);
46 if (err != SkBase64::kNoError) {
47 REPORT_FAILURE(reporter, "err", SkString("SkBase64::Decode failed!"));
48 continue;
49 }
50 REPORTER_ASSERT(reporter, decodeLength == length);
51
52 AutoTMalloc<char> dst(decodeLength);
53 err = SkBase64::Decode(src.get(), actualEncodeLength, dst, &decodeLength);
54 if (err != SkBase64::kNoError) {
55 REPORT_FAILURE(reporter, "err", SkString("SkBase64::Decode failed!"));
56 continue;
57 }
58 REPORTER_ASSERT(reporter, decodeLength == length);
59
60 REPORTER_ASSERT(reporter, (strcmp((const char*) (all + offset), dst.get()) == 0));
61 }
62}
reporter
constexpr int SkToInt(S x)
Definition SkTo.h:29
#define DEF_TEST(name, reporter)
Definition Test.h:312
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
#define REPORT_FAILURE(reporter, cond, message)
Definition Test.h:90
size_t length
Point offset
static Error Decode(const void *src, size_t srcLength, void *dst, size_t *dstLength)
Definition SkBase64.cpp:37
static size_t EncodedSize(size_t srcDataLength)
Definition SkBase64.h:40
@ kNoError
Definition SkBase64.h:16
static size_t Encode(const void *src, size_t length, void *dst, const char *encode=nullptr)
Definition SkBase64.cpp:113