<commit_before>/**
 * @author <NAME>
 * @license The MIT license (LICENSE.txt)
 * @copyright 2015 <NAME>
 * @url https://github.com/rharel/js-gol
 */

var World = require('../src/World').World;

var expect = require('chai').expect;


describe ('World', function() {

  describe ('fresh instance', function() {

    var w = 1, h = 2;
    var world = new World(w, h);

    it ('should have given width and height', function() {

      expect(world.width).to.be.equal(w);
      expect(world.height).to.be.equal(h);
    });

    it ('should have zero population', function() {

      expect(world.population).to.be.equal(0);
    });
  });

  describe ('spawning cells', function() {

    var w = 2; h = 2;
    var world;

    beforeEach(function() {
      world = new World(w, h);
    });

    it ('should be dead before spawn', function() {

      expect(world.get(0, 0)).to.be.equal(false);
    });

    it ('should not spawn out of bounds', function() {

      expect(world.spawn(w, h)).to.be.equal(false);
    });

    it ('should spawn on an empty cell', function() {

      expect(world.spawn(0, 0)).to.be.equal(true);
      expect(world.get(0, 0)).to.be.equal(true);
      expect(world.population).to.be.equal(1);
    });

    it ('should not spawn on an occupied cell', function() {

      expect(world.spawn(0, 0)).to.be.equal(true);
      expect(world.spawn(0, 0)).to.be.equal(false);
    });
  });

  describe ('killing cells', function() {

    var w = 2, h = 2;
    var world;

    beforeEach(function() {
      world = new World(w, h);
    });

    it ('should not kill a dead cell', function() {

      expect(world.kill(0, 0)).to.be.equal(false);
    });

    it ('should not kill out of bounds', function() {

      expect(world.kill(w, h)).to.be.equal(false);
    });

    it ('should kill a living cell', function() {

      world.spawn(0, 0);

      expect(world.kill(0, 0)).to.be.equal(true);
      expect(world.get(0, 0)).to.be.equal(false);
      expect(world.population).to.be.equal(0);
    });
  });

  describe('stepping', function() {

    var w = 5, h = 5;
    var c = 2;

    function populate(world, nNeighbours) {

      if (nNeighbours >= 8) { world.spawn(c - 1, c - 1); }
      if (nNeighbours >= 7) { world.spawn(c - 1, c); }
      if (nNeighbours >= 6) { world.spawn(c - 1, c + 1); }
      if (nNeighbours >= 5) { world.spawn(c, c - 1); }
      if (nNeighbours >= 4) { world.spawn(c, c + 1); }
      if (nNeighbours >= 3) { world.spawn(c + 1, c - 1); }
      if (nNeighbours >= 2) { world.spawn(c + 1, c); }
      if (nNeighbours >= 1) { world.spawn(c + 1, c + 1); }
    }

    function testCase(world, nNeighbours, expectedResult) {

      populate(world, nNeighbours);
      world.step();

      expect(world.get(c, c)).to.be.equal(expectedResult);
    }

    describe ('stepping a live cell', function() {

      var world;

      function reset() {
        world = new World(w, h);
        world.spawn(c, c);
      }

      beforeEach(reset);

      it ('should die if it has fewer than two neighbours', function() {

        testCase(world, 0, false); reset();
        testCase(world, 1, false); reset();
      });

      it ('should live on if it has two or three neighbours', function() {

        testCase(world, 2, true); reset();
        testCase(world, 3, true); reset();
      });

      it ('should die if it has four or more neighbours', function() {

        testCase(world, 4, false); reset();
        testCase(world, 5, false); reset();
        testCase(world, 6, false); reset();
        testCase(world, 7, false); reset();
        testCase(world, 8, false); reset();
      });
    });

    describe ('stepping a dead cell', function() {

      var world;

      function reset() {
        world = new World(w, h);
      }

      beforeEach(reset);

      it ('should spawn if it has three neighbours', function() {

        testCase(world, 3, true); reset();
      });

      it ('should remain dead if it has < 3 or > 3', function() {

        testCase(world, 0, false); reset();
        testCase(world, 1, false); reset();
        testCase(world, 2, false); reset();

        testCase(world, 4, false); reset();
        testCase(world, 5, false); reset();
        testCase(world, 6, false); reset();
        testCase(world, 7, false); reset();
        testCase(world, 8, false); reset();
      });
    });
  });
});
<commit_msg>added new test case for inspect_all and optional worldwrap paramter<commit_after>/**
 * @author <NAME>
 * @license The MIT license (LICENSE.txt)
 * @copyright 2015 <NAME>
 * @url https://github.com/rharel/js-gol
 */

var World = require('../src/World').World;

var expect = require('chai').expect;


describe ('World', function() {

  describe ('default instance', function() {

    var w = 1, h = 2;
    var world = new World(w, h);

    it ('should have given width and height', function() {

      expect(world.width).to.be.equal(w);
      expect(world.height).to.be.equal(h);
    });

    it ('should not be wrapped', function() {

      expect(world.is_wrapped).to.be.equal(false);
    });

    it ('should have zero population', function() {

      expect(world.population).to.be.equal(0);
    });
  });
  describe ('wrapped instance', function() {

    var w = 1, h = 2;
    var world = new World(w, h, true);

    it ('should have given width and height', function() {

      expect(world.width).to.be.equal(w);
      expect(world.height).to.be.equal(h);
    });

    it ('should be wrapped', function() {

      expect(world.is_wrapped).to.be.equal(true);
    });

    it ('should have zero population', function() {

      expect(world.population).to.be.equal(0);
    });
  });

  describe ('spawning cells', function() {

    var w = 2; h = 2;
    var world;

    beforeEach(function() {
      world = new World(w, h);
    });

    it ('should be dead before spawn', function() {

      expect(world.inspect(0, 0)).to.be.equal(false);
    });

    it ('should not spawn out of bounds', function() {

      expect(world.spawn(w, h)).to.be.equal(false);
    });

    it ('should spawn on an empty cell', function() {

      expect(world.spawn(0, 0)).to.be.equal(true);
      expect(world.inspect(0, 0)).to.be.equal(true);
      expect(world.population).to.be.equal(1);
    });

    it ('should not spawn on an occupied cell', function() {

      expect(world.spawn(0, 0)).to.be.equal(true);
      expect(world.spawn(0, 0)).to.be.equal(false);
    });
  });
  describe ('killing cells', function() {

    var w = 2, h = 2;
    var world;

    beforeEach(function() {
      world = new World(w, h);
    });

    it ('should not kill a dead cell', function() {

      expect(world.kill(0, 0)).to.be.equal(false);
    });

    it ('should not kill out of bounds', function() {

      expect(world.kill(w, h)).to.be.equal(false);
    });

    it ('should kill a living cell', function() {

      world.spawn(0, 0);

      expect(world.kill(0, 0)).to.be.equal(true);
      expect(world.inspect(0, 0)).to.be.equal(false);
      expect(world.population).to.be.equal(0);
    });
  });

  describe('evolution', function() {

    var w = 5, h = 5;
    var c = 2;

    function populate(world, nNeighbours) {

      if (nNeighbours >= 8) { world.spawn(c - 1, c - 1); }
      if (nNeighbours >= 7) { world.spawn(c - 1, c); }
      if (nNeighbours >= 6) { world.spawn(c - 1, c + 1); }
      if (nNeighbours >= 5) { world.spawn(c, c - 1); }
      if (nNeighbours >= 4) { world.spawn(c, c + 1); }
      if (nNeighbours >= 3) { world.spawn(c + 1, c - 1); }
      if (nNeighbours >= 2) { world.spawn(c + 1, c); }
      if (nNeighbours >= 1) { world.spawn(c + 1, c + 1); }
    }
    function testCase(world, nNeighbours, expectedResult) {

      populate(world, nNeighbours);
      world.step();

      expect(world.inspect(c, c)).to.be.equal(expectedResult);
    }

    describe ('stepping a live cell', function() {

      var world;

      function reset() {
        world = new World(w, h);
        world.spawn(c, c);
      }

      beforeEach(reset);

      it ('should die if it has fewer than two neighbours', function() {

        testCase(world, 0, false); reset();
        testCase(world, 1, false); reset();
      });

      it ('should live on if it has two or three neighbours', function() {

        testCase(world, 2, true); reset();
        testCase(world, 3, true); reset();
      });

      it ('should die if it has four or more neighbours', function() {

        testCase(world, 4, false); reset();
        testCase(world, 5, false); reset();
        testCase(world, 6, false); reset();
        testCase(world, 7, false); reset();
        testCase(world, 8, false); reset();
      });
    });
    describe ('stepping a dead cell', function() {

      var world;

      function reset() {
        world = new World(w, h);
      }

      beforeEach(reset);

      it ('should spawn if it has three neighbours', function() {

        testCase(world, 3, true); reset();
      });

      it ('should remain dead if it has < 3 or > 3', function() {

        testCase(world, 0, false); reset();
        testCase(world, 1, false); reset();
        testCase(world, 2, false); reset();

        testCase(world, 4, false); reset();
        testCase(world, 5, false); reset();
        testCase(world, 6, false); reset();
        testCase(world, 7, false); reset();
        testCase(world, 8, false); reset();
      });
    });
  });

  describe('inspection', function() {

    var w = 1, h = 3;
    var world = new World(w, h);

    world.spawn(0, 0);
    world.spawn(0, 2);

    it ('should have population == 2', function() {

      expect(world.population).to.be.equal(2);
    });

    it ('should inspect all living cells', function() {

      var visited = [];
      function callback(p) {

        visited.push(p);
      }

      world.inspect_all(callback);

      expect(visited.length).to.be.equal(2);
      visited.forEach(function(p) {

        expect(p.x).to.be.equal(0);
        if (p.y != 0 && p.y != 2) {

          expect.fail();
        }
      });
    });
  });
});

{'fraction_non_alphanumeric': 0.1781951898230968, 'fraction_numerical': 0.016199562711190617, 'mean_word_length': 2.397366644159352, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 25, 'https://': 2, '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': '3555895', 'n_tokens_mistral': 3575, 'n_tokens_neox': 3269, 'n_words': 891}