forked from tensorflow/tfjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon_test.ts
More file actions
149 lines (136 loc) · 5.24 KB
/
common_test.ts
File metadata and controls
149 lines (136 loc) · 5.24 KB
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
144
145
146
147
148
149
/**
* @license
* Copyright 2018 Google LLC
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
* =============================================================================
*/
/**
* Unit tests for common.ts.
*/
import {checkDataFormat, checkPaddingMode, checkPoolMode, getUniqueTensorName, isValidTensorName} from './common';
import {VALID_DATA_FORMAT_VALUES, VALID_PADDING_MODE_VALUES, VALID_POOL_MODE_VALUES} from './keras_format/common';
describe('checkDataFormat', () => {
it('Valid values', () => {
const extendedValues = VALID_DATA_FORMAT_VALUES.concat([undefined, null]);
for (const validValue of extendedValues) {
// Using implicit "expect().toNotThrow()" for valid values
checkDataFormat(validValue);
}
});
it('Invalid values', () => {
// Test invalid values are rejected, and reported in the error.
expect(() => checkDataFormat('foo')).toThrowError(/foo/);
try {
checkDataFormat('bad');
} catch (e) {
expect(e).toMatch('DataFormat');
// Test that the error message contains the list of valid values.
for (const validValue of VALID_DATA_FORMAT_VALUES) {
expect(e).toMatch(validValue);
}
}
});
});
describe('checkPaddingMode', () => {
it('Valid values', () => {
const extendedValues = VALID_PADDING_MODE_VALUES.concat([undefined, null]);
for (const validValue of extendedValues) {
// Using implicit "expect().toNotThrow()" for valid values
checkPaddingMode(validValue);
}
});
it('Invalid values', () => {
// Test invalid values are rejected, and reported in the error.
expect(() => checkPaddingMode('foo')).toThrowError(/foo/);
try {
checkPaddingMode('bad');
} catch (e) {
expect(e).toMatch('PaddingMode');
// Test that the error message contains the list of valid values.
for (const validValue of VALID_PADDING_MODE_VALUES) {
expect(e).toMatch(validValue);
}
}
});
});
describe('checkPoolMode', () => {
it('Valid values', () => {
const extendedValues = VALID_POOL_MODE_VALUES.concat([undefined, null]);
for (const validValue of extendedValues) {
// Using implicit "expect().toNotThrow()" for valid values
checkPoolMode(validValue);
}
});
it('Invalid values', () => {
// Test invalid values are rejected, and reported in the error.
expect(() => checkPoolMode('foo')).toThrowError(/foo/);
try {
checkPoolMode('bad');
} catch (e) {
expect(e).toMatch('PoolMode');
// Test that the error message contains the list of valid values.
for (const validValue of VALID_POOL_MODE_VALUES) {
expect(e).toMatch(validValue);
}
}
});
});
describe('isValidTensorName', () => {
it('Valid tensor names', () => {
expect(isValidTensorName('a')).toEqual(true);
expect(isValidTensorName('A')).toEqual(true);
expect(isValidTensorName('foo1')).toEqual(true);
expect(isValidTensorName('Foo2')).toEqual(true);
expect(isValidTensorName('n_1')).toEqual(true);
expect(isValidTensorName('n.1')).toEqual(true);
expect(isValidTensorName('n_1_2')).toEqual(true);
expect(isValidTensorName('n.1.2')).toEqual(true);
expect(isValidTensorName('a/B/c')).toEqual(true);
expect(isValidTensorName('z_1/z_2/z.3')).toEqual(true);
expect(isValidTensorName('z-1/z-2/z.3')).toEqual(true);
expect(isValidTensorName('1Qux')).toEqual(true);
expect(isValidTensorName('5-conv/kernel')).toEqual(true);
});
it('Invalid tensor names: empty', () => {
expect(isValidTensorName('')).toEqual(false);
});
it('Invalid tensor names: whitespaces', () => {
expect(isValidTensorName('a b')).toEqual(false);
expect(isValidTensorName('ab ')).toEqual(false);
});
it('Invalid tensor names: forbidden characters', () => {
expect(isValidTensorName('-foo1')).toEqual(false);
expect(isValidTensorName('-foo2-')).toEqual(false);
expect(isValidTensorName('bar3!4')).toEqual(false);
});
it('Invalid tensor names: invalid first characters', () => {
expect(isValidTensorName('/foo/bar')).toEqual(false);
expect(isValidTensorName('.baz')).toEqual(false);
expect(isValidTensorName('_baz')).toEqual(false);
});
it('Invalid tensor names: non-ASCII', () => {
expect(isValidTensorName('フ')).toEqual(false);
expect(isValidTensorName('ξ')).toEqual(false);
});
});
describe('getUniqueTensorName', () => {
it('Adds unique suffixes to tensor names', () => {
expect(getUniqueTensorName('xx')).toEqual('xx');
expect(getUniqueTensorName('xx')).toEqual('xx_1');
expect(getUniqueTensorName('xx')).toEqual('xx_2');
expect(getUniqueTensorName('xx')).toEqual('xx_3');
});
it('Correctly handles preexisting unique suffixes on tensor names', () => {
expect(getUniqueTensorName('yy')).toEqual('yy');
expect(getUniqueTensorName('yy')).toEqual('yy_1');
expect(getUniqueTensorName('yy_1')).toEqual('yy_1_1');
expect(getUniqueTensorName('yy')).toEqual('yy_2');
expect(getUniqueTensorName('yy_1')).toEqual('yy_1_2');
expect(getUniqueTensorName('yy_2')).toEqual('yy_2_1');
expect(getUniqueTensorName('yy')).toEqual('yy_3');
expect(getUniqueTensorName('yy_1_1')).toEqual('yy_1_1_1');
});
});