Skip to content

Commit 51dc27a

Browse files
committed
[tfjs-core] test encodeBase64, decodeBase64
1 parent 25398a5 commit 51dc27a

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* @license
3+
* Copyright 2019 Google Inc. All Rights Reserved.
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+
18+
import * as tf from '../index';
19+
import {ALL_ENVS, describeWithFlags} from '../jasmine_util';
20+
import {expectArraysEqual} from '../test_util';
21+
22+
const txtArr = [
23+
'Hello TensorFlow.js!', '𝌆', 'Pre\u2014trained models with Base64 ops\u002e',
24+
'how about these? 🌍💻🍕', 'https://www.tensorflow.org/js', 'àβÇdéf',
25+
'你好, 世界', `Build, train, & deploy
26+
ML models in JS`
27+
];
28+
const urlSafeB64 = [
29+
'SGVsbG8gVGVuc29yRmxvdy5qcyE', '8J2Mhg',
30+
'UHJl4oCUdHJhaW5lZCBtb2RlbHMgd2l0aCBCYXNlNjQgb3BzLg',
31+
'aG93IGFib3V0IHRoZXNlPyDwn4yN8J-Su_CfjZU',
32+
'aHR0cHM6Ly93d3cudGVuc29yZmxvdy5vcmcvanM', 'w6DOssOHZMOpZg',
33+
'5L2g5aW9LCDkuJbnlYw', 'QnVpbGQsIHRyYWluLCAmIGRlcGxveQpNTCBtb2RlbHMgaW4gSlM'
34+
];
35+
const urlSafeB64Pad = [
36+
'SGVsbG8gVGVuc29yRmxvdy5qcyE=', '8J2Mhg==',
37+
'UHJl4oCUdHJhaW5lZCBtb2RlbHMgd2l0aCBCYXNlNjQgb3BzLg==',
38+
'aG93IGFib3V0IHRoZXNlPyDwn4yN8J-Su_CfjZU=',
39+
'aHR0cHM6Ly93d3cudGVuc29yZmxvdy5vcmcvanM=', 'w6DOssOHZMOpZg==',
40+
'5L2g5aW9LCDkuJbnlYw=', 'QnVpbGQsIHRyYWluLCAmIGRlcGxveQpNTCBtb2RlbHMgaW4gSlM='
41+
];
42+
43+
describeWithFlags('encodeBase64', ALL_ENVS, () => {
44+
it('scalar', async () => {
45+
const a = tf.scalar(txtArr[1], 'string');
46+
const r = tf.encodeBase64(a);
47+
expect(r.shape).toEqual([]);
48+
expectArraysEqual(await r.data(), urlSafeB64[1]);
49+
});
50+
it('1D padded', async () => {
51+
const a = tf.tensor1d([txtArr[2]], 'string');
52+
const r = tf.encodeBase64(a, true);
53+
expect(r.shape).toEqual([1]);
54+
expectArraysEqual(await r.data(), [urlSafeB64Pad[2]]);
55+
});
56+
it('2D', async () => {
57+
const a = tf.tensor2d(txtArr, [2, 4], 'string');
58+
const r = tf.encodeBase64(a, false);
59+
expect(r.shape).toEqual([2, 4]);
60+
expectArraysEqual(await r.data(), urlSafeB64);
61+
});
62+
it('3D padded', async () => {
63+
const a = tf.tensor3d(txtArr, [2, 2, 2], 'string');
64+
const r = tf.encodeBase64(a, true);
65+
expect(r.shape).toEqual([2, 2, 2]);
66+
expectArraysEqual(await r.data(), urlSafeB64Pad);
67+
});
68+
});
69+
70+
describeWithFlags('decodeBase64', ALL_ENVS, () => {
71+
it('scalar', async () => {
72+
const a = tf.scalar(urlSafeB64[1], 'string');
73+
const r = tf.decodeBase64(a);
74+
expect(r.shape).toEqual([]);
75+
expectArraysEqual(await r.data(), txtArr[1]);
76+
});
77+
it('1D padded', async () => {
78+
const a = tf.tensor1d([urlSafeB64Pad[2]], 'string');
79+
const r = tf.decodeBase64(a);
80+
expect(r.shape).toEqual([1]);
81+
expectArraysEqual(await r.data(), [txtArr[2]]);
82+
});
83+
it('2D', async () => {
84+
const a = tf.tensor2d(urlSafeB64, [2, 4], 'string');
85+
const r = tf.decodeBase64(a);
86+
expect(r.shape).toEqual([2, 4]);
87+
expectArraysEqual(await r.data(), txtArr);
88+
});
89+
it('3D padded', async () => {
90+
const a = tf.tensor3d(urlSafeB64Pad, [2, 2, 2], 'string');
91+
const r = tf.decodeBase64(a);
92+
expect(r.shape).toEqual([2, 2, 2]);
93+
expectArraysEqual(await r.data(), txtArr);
94+
});
95+
});
96+
97+
describeWithFlags('encodeBase64-decodeBase64', ALL_ENVS, () => {
98+
it('round-trip', async () => {
99+
const s = [txtArr.join('')];
100+
const a = tf.tensor(s, [1], 'string');
101+
const b = tf.encodeBase64(a);
102+
const c = tf.decodeBase64(b);
103+
expectArraysEqual(await c.data(), s);
104+
});
105+
});

tfjs-core/src/tests.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ import './ops/softmax_test';
8989
import './ops/sparse_to_dense_test';
9090
import './ops/spectral_ops_test';
9191
import './ops/strided_slice_test';
92+
import './ops/string_ops_test';
9293
import './ops/topk_test';
9394
import './ops/transpose_test';
9495
import './ops/unary_ops_test';

0 commit comments

Comments
 (0)