Skip to content

Commit dc84858

Browse files
MaleDongtargos
authored andcommitted
test,util: add missing tests and conditions
1) Add missing unit tests by `ucs-2` in different kinds of cases. 2) Add missing unit tests by `usc-2` in different kinds of cases. 3) Fix a bug:We cannot find `ucs-2` in `case 5`'s `if` condition after `toLowerCase()` PR-URL: #21455 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 1044baf commit dc84858

3 files changed

Lines changed: 26 additions & 9 deletions

File tree

‎benchmark/util/normalize-encoding.js‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,25 @@ const common = require('../common.js');
44
constassert=require('assert');
55

66
constgroupedInputs={
7-
group_common: ['undefined','utf8','utf-8','base64','binary','latin1'],
8-
group_upper: ['UTF-8','UTF8','UCS2','UTF-16LE','UTF16LE','BASE64'],
9-
group_uncommon: ['foo','1','false','undefined','[]'],
7+
group_common: ['undefined','utf8','utf-8','base64',
8+
'binary','latin1','ucs-2','usc-2'],
9+
group_upper: ['UTF-8','UTF8','UCS2','UTF-16LE',
10+
'UTF16LE','BASE64','UCS-2','USC-2'],
11+
group_uncommon: ['foo','1','false','undefined','[]','{}'],
1012
group_misc: ['','utf16le','usc2','hex','HEX','BINARY']
1113
};
1214

1315
constinputs=[
14-
'','utf8','utf-8','UTF-8',
15-
'UTF8','Utf8','uTf-8','utF-8','ucs2',
16-
'UCS2','utf16le','utf-16le','UTF-16LE','UTF16LE',
16+
'',
17+
'utf8','utf-8','UTF-8',
18+
'UTF8','Utf8','uTf-8','utF-8',
19+
'ucs2','UCS2','UcS2',
20+
'USC2','usc2','uSc2',
21+
'ucs-2','UCS-2','UcS-2',
22+
'usc-2','USC-2','uSc-2',
23+
'utf16le','utf-16le','UTF-16LE','UTF16LE',
1724
'binary','BINARY','latin1','base64','BASE64',
18-
'hex','HEX','foo','1','false','undefined','[]'];
25+
'hex','HEX','foo','1','false','undefined','[]','{}'];
1926

2027
constbench=common.createBenchmark(main,{
2128
input: inputs.concat(Object.keys(groupedInputs)),
@@ -42,6 +49,8 @@ function getInput(input) {
4249
return[undefined];
4350
case'[]':
4451
return[[]];
52+
case'{}':
53+
return[{}];
4554
default:
4655
return[input];
4756
}
@@ -53,7 +62,7 @@ function main({ input, n }) {
5362
varnoDead='';
5463

5564
bench.start();
56-
for(vari=0;i<n;i+=1){
65+
for(vari=0;i<n;++i){
5766
for(varj=0;j<inputs.length;++j){
5867
noDead=normalizeEncoding(inputs[j]);
5968
}

‎lib/internal/util.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ function slowCases(enc) {
115115
if(enc==='ucs2'||enc==='UCS2')return'utf16le';
116116
enc=`${enc}`.toLowerCase();
117117
if(enc==='utf8')return'utf8';
118-
if(enc==='ucs2'||enc==='UCS2')return'utf16le';
118+
if(enc==='ucs2')return'utf16le';
119119
break;
120120
case3:
121121
if(enc==='hex'||enc==='HEX'||`${enc}`.toLowerCase()==='hex')
@@ -131,6 +131,7 @@ function slowCases(enc) {
131131
if(enc==='utf-8')return'utf8';
132132
if(enc==='ascii')return'ascii';
133133
if(enc==='usc-2')return'utf16le';
134+
if(enc==='ucs-2')return'utf16le';
134135
break;
135136
case6:
136137
if(enc==='base64')return'base64';

‎test/parallel/test-internal-util-normalizeencoding.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,16 @@ const tests = [
1818
['utF-8','utf8'],
1919
['ucs2','utf16le'],
2020
['UCS2','utf16le'],
21+
['ucs-2','utf16le'],
22+
['UCS-2','utf16le'],
23+
['UcS-2','utf16le'],
2124
['utf16le','utf16le'],
2225
['utf-16le','utf16le'],
2326
['UTF-16LE','utf16le'],
2427
['UTF16LE','utf16le'],
28+
['usc-2','utf16le'],
29+
['USC-2','utf16le'],
30+
['uSc-2','utf16le'],
2531
['binary','latin1'],
2632
['BINARY','latin1'],
2733
['latin1','latin1'],
@@ -36,6 +42,7 @@ const tests = [
3642
[NaN,undefined],
3743
[0,undefined],
3844
[[],undefined],
45+
[{},undefined]
3946
];
4047

4148
tests.forEach((e,i)=>{

0 commit comments

Comments
 (0)