Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fs: add tests for working with typed arrays
  • Loading branch information
Sarat Addepalli committed Aug 20, 2018
commit 9087cc904e24514bc33b821deb4f45dd986a9a32
51 changes: 51 additions & 0 deletions test/parallel/test-fs-file-typedarrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const join = require('path').join;
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

const filename = join(tmpdir.path, 'test.txt');

const s = '南越国是前203年至前111年存在于岭南地区的一个国家,国都位于番禺,疆域包括今天中国的广东、' +
'广西两省区的大部份地区,福建省、湖南、贵州、云南的一小部份地区和越南的北部。' +
'南越国是秦朝灭亡后,由南海郡尉赵佗于前203年起兵兼并桂林郡和象郡后建立。' +
'前196年和前179年,南越国曾先后两次名义上臣属于西汉,成为西汉的“外臣”。前112年,' +
'南越国末代君主赵建德与西汉发生战争,被汉武帝于前111年所灭。南越国共存在93年,' +
'历经五代君主。南越国是岭南地区的第一个有记载的政权国家,采用封建制和郡县制并存的制度,' +
'它的建立保证了秦末乱世岭南地区社会秩序的稳定,有效的改善了岭南地区落后的政治、##济现状。\n';

// length must be a multiple of 8?
const expectStr = s.padEnd(s.length % 8, '-');
const expectBuf = Buffer.from(expectStr, 'utf8');

const types = [
Int8Array,
Uint8Array,
Uint8ClampedArray,
Int16Array,
Uint16Array,
Int32Array,
Uint32Array,
Float32Array,
Float64Array,
DataView
];

const convertToType = (arrayBufferType, input) =>
arrayBufferType.from(Buffer.from(input));
const readInput = (filename) => fs.readFileSync(filename);


for (const type of types) {
const expect = convertToType(type, expectBuf);
fs.writeFileSync(filename, expect);
assert.strictEqual(convertToType(type, readInput(filename)), expect);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benjamingr this test is currently failing with

assert.js:84
  throw new AssertionError(obj);
  ^

AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:
+ actual - expected ... Lines skipped

  Int8Array [
+   45,
+   50,
+   55,
+   44,
+   45,
+   49,
+   49,
+   53,
+   44,
+   45,
...
-   -27,
-   -115,
-   -105,
-   -24,
-   -74,
-   -118,
-   -27,
-   -101,
-   -67,
-   -26,
...

What am i doing wrong?


fs.writeFile(filename, expect, common.mustCall((e) => {
assert.ifError(e);

assert.strictEqual(convertToType(type, readInput(filename)), expect);
}));
}