<commit_before>"use strict";
var assert = require('assert');
var t = require('../index');
var React = require('react/addons');

var errs = t.errs;
var Any = t.Any;
var Nil = t.Nil;
var Bool = t.Bool;
var Num = t.Num;
var Str = t.Str;
var Arr = t.Arr;
var Obj = t.Obj;
var Func = t.Func;
var Err = t.Err;
var Re = t.Re;
var Dat = t.Dat;
var struct = t.struct;
var enums = t.enums;
var union = t.union;
var tuple = t.tuple;
var maybe = t.maybe;
var subtype = t.subtype;
var list = t.list;
var dict = t.dict;
var func = t.func;
var getName = t.util.getName;
var getKind = t.util.getKind;
var mixin = t.util.mixin;
var format = t.util.format;
var namespace = t.util.namespace;
var defaultUpdate = t.util.defaultUpdate;

//
// setup
//

var ok = function (x) { assert.strictEqual(true, x); };
var ko = function (x) { assert.strictEqual(false, x); };
var eq = assert.deepEqual;
var throwsWithMessage = function (f, message) {
    assert['throws'](f, function (err) {
        ok(err instanceof Error);
        eq(err.message, message);
        return true;
    });
};
var doesNotThrow = assert.doesNotThrow;

var noop = function () {};
var Point = struct({
    x: Num,
    y: Num
});

describe('namespace(path, [module])', function () {

    it('should returns a namespace', function () {
        eq(namespace('a'), {a: {}});
        eq(namespace('a.b'), {a: {b: {}}});
        eq(namespace('a.b.0'), {a: {b: {0: {}}}});
    });

    it('should set a module', function () {
        eq(namespace('a.b', 1), {a: {b: 1}});
        eq(namespace('a.b.1.2', 4), {a: {b: {1: {2: 4}}}});
    });

});

describe('defaultUpdate', function () {

    var Tuple = tuple([Str, Num]);
    var List = list(Num);
    var Dict = dict(Str, Num);

    describe('structs', function () {
        
        var instance = new Point({x: 0, y: 1});

        it('should returns the same struct if spec is not defined', function () {
            var updated = defaultUpdate(instance);
            ok(updated === instance);
        });
        it('should handle $set command', function () {
            var updated = defaultUpdate(instance, {x: {$set: 1}});
            eq(updated, {x: 1, y: 1});
        });
        it('should handle $apply command', function () {
            var updated = defaultUpdate(instance, {x: {$apply: function (x) {
                return x + 2;
            }}});
            eq(updated, {x: 2, y: 1});
        });
    });

    describe('tuples', function () { 

        var instance = Tuple(['a', 1]);

        it('should handle $set command', function () {
            var updated = defaultUpdate(instance, {0: {$set: 'b'}});
            eq(updated, ['b', 1]);
        });
    });

    describe('lists', function () { 

        var instance = List([1, 2, 3, 4]);

        it('should handle $set command', function () {
            var updated = defaultUpdate(instance, {2: {$set: 5}});
            eq(updated, [1, 2, 5, 4]);
        });
        it('should handle $splice command', function () {
            var updated = defaultUpdate(instance, {$splice: [1, 2, 5, 6]});
            eq(updated, [1, 5, 6, 4]);
        });
        it('should handle $push command', function () {
            var updated = defaultUpdate(instance, {$push: 5});
            eq(updated, [1, 2, 3, 4, 5]);
            updated = defaultUpdate(instance, {$push: [5, 6]});
            eq(updated, [1, 2, 3, 4, 5, 6]);
        });
        it('should handle $unshift command', function () {
            var updated = defaultUpdate(instance, {$unshift: 5});
            eq(updated, [5, 1, 2, 3, 4]);
            updated = defaultUpdate(instance, {$unshift: [5, 6]});
            eq(updated, [5, 6, 1, 2, 3, 4]);
        });
        it('should handle $swap command', function () {
            var updated = defaultUpdate(instance, {$swap: {from: 1, to: 2}});
            eq(updated, [1, 3, 2, 4]);
        });
    });

    describe('dicts', function () { 

        var instance = Dict({a: 1, b: 2});

        it('should handle $set command', function () {
            var updated = defaultUpdate(instance, {a: {$set: 2}});
            eq(updated, {a: 2, b: 2});
        });
        it('should handle $remove command', function () {
            var updated = defaultUpdate(instance, {a: {$remove: true}});
            eq(updated, {b: 2});
        });
    });

    describe('all together now', function () { <commit_msg>[test] memory saving in update operations<commit_after>"use strict";
var assert = require('assert');
var t = require('../index');
var React = require('react/addons');

var errs = t.errs;
var Any = t.Any;
var Nil = t.Nil;
var Bool = t.Bool;
var Num = t.Num;
var Str = t.Str;
var Arr = t.Arr;
var Obj = t.Obj;
var Func = t.Func;
var Err = t.Err;
var Re = t.Re;
var Dat = t.Dat;
var struct = t.struct;
var enums = t.enums;
var union = t.union;
var tuple = t.tuple;
var maybe = t.maybe;
var subtype = t.subtype;
var list = t.list;
var dict = t.dict;
var func = t.func;
var getName = t.util.getName;
var getKind = t.util.getKind;
var mixin = t.util.mixin;
var format = t.util.format;
var namespace = t.util.namespace;
var defaultUpdate = t.util.defaultUpdate;

//
// setup
//

var ok = function (x) { assert.strictEqual(true, x); };
var ko = function (x) { assert.strictEqual(false, x); };
var eq = assert.deepEqual;
var throwsWithMessage = function (f, message) {
    assert['throws'](f, function (err) {
        ok(err instanceof Error);
        eq(err.message, message);
        return true;
    });
};
var doesNotThrow = assert.doesNotThrow;

var noop = function () {};
var Point = struct({
    x: Num,
    y: Num
});

describe('namespace(path, [module])', function () {

    it('should returns a namespace', function () {
        eq(namespace('a'), {a: {}});
        eq(namespace('a.b'), {a: {b: {}}});
        eq(namespace('a.b.0'), {a: {b: {0: {}}}});
    });

    it('should set a module', function () {
        eq(namespace('a.b', 1), {a: {b: 1}});
        eq(namespace('a.b.1.2', 4), {a: {b: {1: {2: 4}}}});
    });

});

describe('defaultUpdate', function () {

    var Tuple = tuple([Str, Num]);
    var List = list(Num);
    var Dict = dict(Str, Num);

    describe('structs', function () {
        
        var instance = new Point({x: 0, y: 1});

        it('should returns the same struct if spec is not defined', function () {
            var updated = defaultUpdate(instance);
            ok(updated === instance);
        });
        it('should handle $set command', function () {
            var updated = defaultUpdate(instance, {x: {$set: 1}});
            eq(updated, {x: 1, y: 1});
        });
        it('should handle $apply command', function () {
            var updated = defaultUpdate(instance, {x: {$apply: function (x) {
                return x + 2;
            }}});
            eq(updated, {x: 2, y: 1});
        });
    });

    describe('tuples', function () { 

        var instance = Tuple(['a', 1]);

        it('should handle $set command', function () {
            var updated = defaultUpdate(instance, {0: {$set: 'b'}});
            eq(updated, ['b', 1]);
        });
    });

    describe('lists', function () { 

        var instance = List([1, 2, 3, 4]);

        it('should handle $set command', function () {
            var updated = defaultUpdate(instance, {2: {$set: 5}});
            eq(updated, [1, 2, 5, 4]);
        });
        it('should handle $splice command', function () {
            var updated = defaultUpdate(instance, {$splice: [1, 2, 5, 6]});
            eq(updated, [1, 5, 6, 4]);
        });
        it('should handle $push command', function () {
            var updated = defaultUpdate(instance, {$push: 5});
            eq(updated, [1, 2, 3, 4, 5]);
            updated = defaultUpdate(instance, {$push: [5, 6]});
            eq(updated, [1, 2, 3, 4, 5, 6]);
        });
        it('should handle $unshift command', function () {
            var updated = defaultUpdate(instance, {$unshift: 5});
            eq(updated, [5, 1, 2, 3, 4]);
            updated = defaultUpdate(instance, {$unshift: [5, 6]});
            eq(updated, [5, 6, 1, 2, 3, 4]);
        });
        it('should handle $swap command', function () {
            var updated = defaultUpdate(instance, {$swap: {from: 1, to: 2}});
            eq(updated, [1, 3, 2, 4]);
        });
    });

    describe('dicts', function () { 

        var instance = Dict({a: 1, b: 2});

        it('should handle $set command', function () {
            var updated = defaultUpdate(instance, {a: {$set: 2}});
            eq(updated, {a: 2, b: 2});
        });
        it('should handle $remove command', function () {
            var updated = defaultUpdate(instance, {a: {$remove: true}});
            eq(updated, {b: 2});
        });
    });

    describe('memory saving', function () {
        it('should reuse members that are not updated', function () {
            var Struct = struct({
                a: Num,
                b: Str,
                c: tuple([Num, Num]),
            });
            var List = list(Struct);
            var instance = List([{
                a: 1,
                b: 'one',
                c: [1000, 1000000]
            },{
                a: 2,
                b: 'two',
                c: [2000, 2000000]
            }]);

            var updated = defaultUpdate(instance, {
                1: {
                    a: {$set: 119}
                }
            });

            assert.strictEqual(updated[0], instance[0]);
            assert.notStrictEqual(updated[1], instance[1]);

            assert.strictEqual(updated[1].c, instance[1].c);


        });
    });

    describe('all together now', function () { 
{'fraction_non_alphanumeric': 0.18117817579459572, 'fraction_numerical': 0.019877834144321357, 'mean_word_length': 2.098139833226427, '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': '7167823', 'n_tokens_mistral': 3210, 'n_tokens_neox': 2993, 'n_words': 903}