<commit_before>'use strict';

var test = require('tape');
var Format = require('../index');

test('All read/write methods', function(t) {
	const object = {
		uint8_test: 42,
		uint16BE_test: 1001,
		uint16LE_test: 1001,
		uint32BE_test: 500000,
		uint32LE_test: 500000,

		int8_test: -42,
		int16BE_test: -1001,
		int16LE_test: -1001,
		int32BE_test: -500000,
		int32LE_test: -500000,

		floatBE_test: 0.123456,
		floatLE_test: 0.123456,

		doubleBE_test: 0.1234567891,
		doubleLE_test: 0.1234567891,

		buffer_test: new Buffer('deadbeef', 'hex'),
	};

	var fmt = new Format()
		.uint8('uint8_test')
		.uint16BE('uint16BE_test')
		.uint16LE('uint16LE_test')
		.uint32BE('uint32BE_test')
		.uint32LE('uint32LE_test')

		.int8('int8_test')
		.int16BE('int16BE_test')
		.int16LE('int16LE_test')
		.int32BE('int32BE_test')
		.int32LE('int32LE_test')

		.floatBE('floatBE_test')
		.floatLE('floatLE_test')
		.doubleBE('doubleBE_test')
		.doubleLE('doubleLE_test')
		.buffer('buffer_test', 4)
		;

	var output = fmt.write(object);
	var parsed = fmt.parse(output);

	// fixes float inaccuracy for more convenient testing
	parsed.floatBE_test = Math.round(parsed.floatBE_test * 1000000) / 1000000;
	parsed.floatLE_test = Math.round(parsed.floatLE_test * 1000000) / 1000000;

	t.deepEquals(object, parsed, 'Serialization OK');
	t.end();
});

test('Simple', function(t) {
	const buf = new Buffer('ab', 'hex');
	var fmt = new Format().uint8('test');
	var object = fmt.parse(buf);
	var output = fmt.write(object);

	t.equals(object.test, 0xab, 'Parsing OK');
	t.deepEquals(output, buf, 'Output value OK');
	t.end();
});

test('List', function(t) {
	const buf = new Buffer('baadf00dff01ff02ff03ff04', 'hex');

	var fmt = new Format()
		.buffer('header', 4)
		.list('list', 4, new Format()
			.uint8('hdr')
			.uint8('val'));

	var object = fmt.parse(buf);
	var output = fmt.write(object);

	t.deepEquals(object.header, new Buffer('baadf00d', 'hex'), 'Header OK');
	t.deepEquals(object.list, [
		{ hdr: 255, val: 1 },
		{ hdr: 255, val: 2 },
		{ hdr: 255, val: 3 },
		{ hdr: 255, val: 4 }
	], 'Parsed list OK');
	t.deepEquals(output, buf, 'Output value OK');
	t.end();
});

test('Nested', function(t) {
	const buf = new Buffer('0102a1a2', 'hex');<commit_msg>Add: test for length prediction<commit_after>'use strict';

var test = require('tape');
var Format = require('../index');

test('All read/write methods', function(t) {
	const object = {
		uint8_test: 42,
		uint16BE_test: 1001,
		uint16LE_test: 1001,
		uint32BE_test: 500000,
		uint32LE_test: 500000,

		int8_test: -42,
		int16BE_test: -1001,
		int16LE_test: -1001,
		int32BE_test: -500000,
		int32LE_test: -500000,

		floatBE_test: 0.123456,
		floatLE_test: 0.123456,

		doubleBE_test: 0.1234567891,
		doubleLE_test: 0.1234567891,

		buffer_test: new Buffer('deadbeef', 'hex'),
	};

	var fmt = new Format()
		.uint8('uint8_test')
		.uint16BE('uint16BE_test')
		.uint16LE('uint16LE_test')
		.uint32BE('uint32BE_test')
		.uint32LE('uint32LE_test')

		.int8('int8_test')
		.int16BE('int16BE_test')
		.int16LE('int16LE_test')
		.int32BE('int32BE_test')
		.int32LE('int32LE_test')

		.floatBE('floatBE_test')
		.floatLE('floatLE_test')
		.doubleBE('doubleBE_test')
		.doubleLE('doubleLE_test')
		.buffer('buffer_test', 4)
		;

	var output = fmt.write(object);
	var parsed = fmt.parse(output);

	// fixes float inaccuracy for more convenient testing
	parsed.floatBE_test = Math.round(parsed.floatBE_test * 1000000) / 1000000;
	parsed.floatLE_test = Math.round(parsed.floatLE_test * 1000000) / 1000000;

	t.deepEquals(object, parsed, 'Serialization OK');
	t.end();
});

test('Simple', function(t) {
	const buf = new Buffer('ab', 'hex');
	var fmt = new Format().uint8('test');
	var object = fmt.parse(buf);
	var output = fmt.write(object);

	t.equals(object.test, 0xab, 'Parsing OK');
	t.deepEquals(output, buf, 'Output value OK');
	t.end();
});

test('List', function(t) {
	const buf = new Buffer('baadf00dff01ff02ff03ff04', 'hex');

	var fmt = new Format()
		.buffer('header', 4)
		.list('list', 4, new Format()
			.uint8('hdr')
			.uint8('val'));

	t.equals(fmt.length(), 4 + 4 * 2, 'Length prediction OK');

	var object = fmt.parse(buf);
	var output = fmt.write(object);

	t.deepEquals(object.header, new Buffer('baadf00d', 'hex'), 'Header OK');
	t.deepEquals(object.list, [
		{ hdr: 255, val: 1 },
		{ hdr: 255, val: 2 },
		{ hdr: 255, val: 3 },
		{ hdr: 255, val: 4 }
	], 'Parsed list OK');
	t.deepEquals(output, buf, 'Output value OK');
	t.end();
});

test('Nested', function(t) {
	const buf = new Buffer('0102a1a2', 'hex');
{'fraction_non_alphanumeric': 0.17779724107729364, 'fraction_numerical': 0.08955550689730676, 'mean_word_length': 4.438095238095238, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2429996', 'n_tokens_mistral': 2187, 'n_tokens_neox': 1800, 'n_words': 376}