<commit_before>import TextureManager from '../../src/texture-manager';

describe( 'TextureManager: constructor behaves correctly', () => {
  it( 'instantiates correctly', () => {
    const tm = new TextureManager( 256 );
    expect( tm ).to.be.an('object');
    expect( tm ).to.be.an.instanceof( TextureManager );
  });

  it( 'has the property textureSize', () => {
    const tm = new TextureManager( 256 );
    expect( tm ).to.have.property('textureSize');
    expect( tm.textureSize ).to.be.a('number').that.equals( 256 );
  });

  it( 'power of two texture sizes are accepted', () => {
    [ 128, 256, 512, 1024, 2048, 4096, 8192, 16384 ].forEach( (size) => {
      const tm = new TextureManager( size );
      expect( tm.textureSize ).to.be.a('number').that.equals( size );
    });
  });

  it( 'textureSize defaults to 1024 on invalid textureSize (1)', () => {
    const tm = new TextureManager( 'plonk' );
    expect( tm.textureSize ).to.be.a('number').that.equals( 1024 );
  });

  it( 'textureSize defaults to 1024 on invalid textureSize (2)', () => {
    const tm = new TextureManager( 123 );
    expect( tm.textureSize ).to.be.a('number').that.equals( 1024 );
  });
});

describe( 'TextureManager: the debug code works', () => {
  const tm = new TextureManager( 256 );
  tm.debug = true; // Make sure debug code goes through testing
  let knapsack;

  it( 'Logs about knapsack creation', () => {
    const logStub = stub( console, 'log' );
    knapsack = tm._addKnapsack( 256 );
    expect( logStub ).to.have.been.calledWithExactly( 'TextureManager: allocated 256px texture map #1' );
    tm._addKnapsack( 256 );
    expect( logStub ).to.have.been.calledWithExactly( 'TextureManager: allocated 256px texture map #2' );
  });

  it( 'Can still allocate a node correctly, and draws outlines in it', () => {
    // Slightly more extensive tests of this drawing done elsewhere
    const strokeRectSpy = spy( knapsack.rootNode.context, 'strokeRect' );
    const node = tm.allocate( 256, 128 );
    expect( node.rectangle.left ).to.equal( 0 );
    expect( node.rectangle.top ).to.equal( 0 );
    expect( node.rectangle.right ).to.equal( 256 );
    expect( node.rectangle.bottom ).to.equal( 128 );
    expect( node.uvCoordinates() ).to.deep.equal( [ 0, 0.5, 1, 1 ] );
    expect( strokeRectSpy ).to.have.been.called;
  });
});

describe( 'TextureManager: the generator for allocateASync() and solveASync() works', function() {
  const tm = new TextureManager( 256 );

  it( '.solveASync() initially throws an error', () => {
    expect( () => { tm.solveASync() } ).to.throw( Error, `hasn't been set up` );
  });

  it( 'Can allocate nodes asynchronously', () => {
    tm.allocateASync( 128, 32 );
    tm.allocateASync( 32364, 323432 );
    tm.allocateASync( 64, 32 );
    // Note: the failing 2nd node is not a problem as it fails
    // early and returns its failure to the caller directly.
    const solve = tm.solveASync();
    return expect( solve ).to.eventually.resolve;
  });

  it( 'Handles allocation failure, rejecting on the caller', () => {
    const failing = tm.allocateASync( 323643, 3234323 );
    return expect( failing ).to.eventually.be.rejectedWith( Error, 'too large' );
  });

  it( 'Handles 100% allocation failure, rejecting on .solveASync()', () => {
    tm.allocateASync( 323643, 3234323 );
    tm.allocateASync( 323643, 3234323 );
    const solve = tm.solveASync();
    return expect( solve ).to.eventually.resolve;
  });
});

describe( 'TextureManager: multiple knapsack allocation', () => {
  const tm = new TextureManager( 256 );
  let one, two, three, four, five, six, seven, eight;

  it( 'has no knapsacks to start with', () => {
    expect( tm.knapsacks.length ).to.equal( 0 );
  });

  it( 'fails to allocates a node too large (horizontally) asynchronously', () => {
    return expect( tm.allocateNode( tm.textureSize + 1, 1 ) ).to.eventually.be.rejectedWith( Error, 'too large' );
  });

  it( 'fails to allocates a node too large (horizontally)', () => {
    expect( () => { tm.allocate( tm.textureSize + 1, 1 ) } ).to.throw( Error, 'too large' );
  });
<commit_msg>Boost code coverage to 100% branches for TextureManager :)<commit_after>import TextureManager from '../../src/texture-manager';

describe( 'TextureManager: constructor behaves correctly', () => {
  it( 'instantiates correctly', () => {
    const tm = new TextureManager( 256 );
    expect( tm ).to.be.an('object');
    expect( tm ).to.be.an.instanceof( TextureManager );
  });

  it( 'has the property textureSize', () => {
    const tm = new TextureManager( 256 );
    expect( tm ).to.have.property('textureSize');
    expect( tm.textureSize ).to.be.a('number').that.equals( 256 );
  });

  it( 'power of two texture sizes are accepted', () => {
    [ 128, 256, 512, 1024, 2048, 4096, 8192, 16384 ].forEach( (size) => {
      const tm = new TextureManager( size );
      expect( tm.textureSize ).to.be.a('number').that.equals( size );
    });
  });

  it( 'textureSize defaults to 1024 on invalid textureSize (1)', () => {
    const tm = new TextureManager( 'plonk' );
    expect( tm.textureSize ).to.be.a('number').that.equals( 1024 );
  });

  it( 'textureSize defaults to 1024 on invalid textureSize (2)', () => {
    const tm = new TextureManager( 123 );
    expect( tm.textureSize ).to.be.a('number').that.equals( 1024 );
  });
});

describe( 'TextureManager: the debug code works', () => {
  const tm = new TextureManager( 256 );
  tm.debug = true; // Make sure debug code goes through testing
  let knapsack;

  it( 'Logs about knapsack creation', () => {
    const logStub = stub( console, 'log' );
    knapsack = tm._addKnapsack( 256 );
    expect( logStub ).to.have.been.calledWithExactly( 'TextureManager: allocated 256px texture map #1' );
    tm._addKnapsack( 256 );
    expect( logStub ).to.have.been.calledWithExactly( 'TextureManager: allocated 256px texture map #2' );
  });

  it( 'Can still allocate a node correctly, and draws outlines in it', () => {
    // Slightly more extensive tests of this drawing done elsewhere
    const strokeRectSpy = spy( knapsack.rootNode.context, 'strokeRect' );
    const node = tm.allocate( 256, 128 );
    expect( node.rectangle.left ).to.equal( 0 );
    expect( node.rectangle.top ).to.equal( 0 );
    expect( node.rectangle.right ).to.equal( 256 );
    expect( node.rectangle.bottom ).to.equal( 128 );
    expect( node.uvCoordinates() ).to.deep.equal( [ 0, 0.5, 1, 1 ] );
    expect( strokeRectSpy ).to.have.been.called;
  });
});

describe( 'TextureManager: the generator for allocateASync() and solveASync() works', function() {
  const tm = new TextureManager( 256 );

  it( '.solveASync() initially throws an error', () => {
    expect( () => { tm.solveASync() } ).to.throw( Error, `hasn't been set up` );
  });

  it( 'Can allocate nodes asynchronously', () => {
    tm.allocateASync( 128, 32 );
    tm.allocateASync( 32364, 323432 );
    tm.allocateASync( 64, 32 );
    // Note: the failing 2nd node is not a problem as it fails
    // early and returns its failure to the caller directly.
    const solve = tm.solveASync();
    return expect( solve ).to.eventually.resolve;
  });

  it( 'Handles allocation failure, rejecting on the caller', () => {
    const failing = tm.allocateASync( 323643, 3234323 );
    return expect( failing ).to.eventually.be.rejectedWith( Error, 'too large' );
  });

  it( 'Handles 100% allocation failure, rejecting on .solveASync()', () => {
    tm.allocateASync( 323643, 3234323 );
    tm.allocateASync( 323643, 3234323 );
    const solve = tm.solveASync();
    return expect( solve ).to.eventually.resolve;
  });
});

describe( 'TextureManager: multiple knapsack allocation', () => {
  const tm = new TextureManager( 256 );
  let one, two, three, four, five, six, seven, eight;

  it( 'is safe to call release without a node', () => {
    expect( tm.release() ).to.equal.undefined;
  });

  it( 'has no knapsacks to start with', () => {
    expect( tm.knapsacks.length ).to.equal( 0 );
  });

  it( 'fails to allocates a node too large (horizontally) asynchronously', () => {
    return expect( tm.allocateNode( tm.textureSize + 1, 1 ) ).to.eventually.be.rejectedWith( Error, 'too large' );
  });

  it( 'fails to allocates a node too large (horizontally)', () => {
    expect( () => { tm.allocate( tm.textureSize + 1, 1 ) } ).to.throw( Error, 'too large' );
  });

{'fraction_non_alphanumeric': 0.16202652646672241, 'fraction_numerical': 0.04098458597203967, 'mean_word_length': 3.3821989528795813, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 44, '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': '2825345', 'n_tokens_mistral': 3020, 'n_tokens_neox': 2694, 'n_words': 812}