forked from tensorflow/tfjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform_node_test.ts
143 lines (118 loc) · 4.88 KB
/
platform_node_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
import * as tf from '../index';
import {describeWithFlags, NODE_ENVS} from '../jasmine_util';
import * as platform_node from './platform_node';
import {PlatformNode} from './platform_node';
describeWithFlags('PlatformNode', NODE_ENVS, () => {
it('fetch should use global.fetch if defined', async () => {
const globalFetch = tf.env().global.fetch;
spyOn(tf.env().global, 'fetch').and.returnValue(() => {});
const platform = new PlatformNode();
await platform.fetch('test/url', {method: 'GET'});
expect(tf.env().global.fetch).toHaveBeenCalledWith('test/url', {
method: 'GET'
});
tf.env().global.fetch = globalFetch;
});
it('fetch should use node-fetch with tf.env().global.fetch is null',
async () => {
const globalFetch = tf.env().global.fetch;
tf.env().global.fetch = null;
const platform = new PlatformNode();
const savedFetch = platform_node.getSystemFetch();
// Null out the system fetch so we force it to require node-fetch.
platform_node.resetSystemFetch();
const testFetch = {fetch: (url: string, init: RequestInit) => () => {}};
// Mock the actual fetch call.
spyOn(testFetch, 'fetch').and.returnValue(() => {});
// Mock the import to override the real require of node-fetch.
spyOn(platform_node.getNodeFetch, 'importFetch')
.and.callFake(
() => (url: string, init: RequestInit) =>
testFetch.fetch(url, init));
await platform.fetch('test/url', {method: 'GET'});
expect(platform_node.getNodeFetch.importFetch).toHaveBeenCalled();
expect(testFetch.fetch).toHaveBeenCalledWith('test/url', {
method: 'GET'
});
platform_node.setSystemFetch(savedFetch);
tf.env().global.fetch = globalFetch;
});
it('now should use process.hrtime', async () => {
const time: [number, number] = [100, 200];
spyOn(process, 'hrtime').and.returnValue(time);
expect(tf.env().platform.now()).toEqual(time[0] * 1000 + time[1] / 1000000);
});
it('encodeUTF8 single string', () => {
const platform = new PlatformNode();
const bytes = platform.encode('hello', 'utf-8');
expect(bytes.length).toBe(5);
expect(bytes).toEqual(new Uint8Array([104, 101, 108, 108, 111]));
});
it('encodeUTF8 two strings delimited', () => {
const platform = new PlatformNode();
const bytes = platform.encode('hello\x00world', 'utf-8');
expect(bytes.length).toBe(11);
expect(bytes).toEqual(
new Uint8Array([104, 101, 108, 108, 111, 0, 119, 111, 114, 108, 100]));
});
it('encodeUTF8 cyrillic', () => {
const platform = new PlatformNode();
const bytes = platform.encode('Здраво', 'utf-8');
expect(bytes.length).toBe(12);
expect(bytes).toEqual(new Uint8Array(
[208, 151, 208, 180, 209, 128, 208, 176, 208, 178, 208, 190]));
});
it('decode single string', () => {
const platform = new PlatformNode();
const s =
platform.decode(new Uint8Array([104, 101, 108, 108, 111]), 'utf8');
expect(s.length).toBe(5);
expect(s).toEqual('hello');
});
it('decode two strings delimited', () => {
const platform = new PlatformNode();
const s = platform.decode(
new Uint8Array([104, 101, 108, 108, 111, 0, 119, 111, 114, 108, 100]),
'utf8');
expect(s.length).toBe(11);
expect(s).toEqual('hello\x00world');
});
it('decode cyrillic', () => {
const platform = new PlatformNode();
const s = platform.decode(
new Uint8Array(
[208, 151, 208, 180, 209, 128, 208, 176, 208, 178, 208, 190]),
'utf8');
expect(s.length).toBe(6);
expect(s).toEqual('Здраво');
});
it('isTypedArray returns false if not a typed array', () => {
const platform = new PlatformNode();
expect(platform.isTypedArray([1, 2, 3])).toBeFalse();
});
for (const typedArrayConstructor of [Float32Array, Int32Array, Uint8Array,
Uint8ClampedArray]) {
it(`isTypedArray returns true if it is a ${typedArrayConstructor.name}`,
() => {
const platform = new PlatformNode();
const array = new typedArrayConstructor([1,2,3]);
expect(platform.isTypedArray(array)).toBeTrue();
});
}
});