<commit_before>$:.unshift File.join(File.dirname(__FILE__),'..','lib')

require 'test/unit'
require 'wavefile.rb'

include WaveFile

class WriterTest < Test::Unit::TestCase
  OUTPUT_FOLDER = "test/fixtures/actual_output"

  SQUARE_WAVE_CYCLE = {}
  SQUARE_WAVE_CYCLE[:mono] = {}
  SQUARE_WAVE_CYCLE[:mono][8] =    [88, 88, 88, 88, 167, 167, 167, 167]
  SQUARE_WAVE_CYCLE[:mono][16] =   [-10000, -10000, -10000, -10000, 10000, 10000, 10000, 10000]
  SQUARE_WAVE_CYCLE[:mono][32] =   [-1000000000, -1000000000, -1000000000, -1000000000,
                                     1000000000, 1000000000, 1000000000, 1000000000]
  SQUARE_WAVE_CYCLE[:stereo] = {}
  SQUARE_WAVE_CYCLE[:stereo][8] =  [[88, 88], [88, 88], [88, 88], [88, 88],
                                    [167, 167], [167, 167], [167, 167], [167, 167]]
  SQUARE_WAVE_CYCLE[:stereo][16] = [[-10000, -10000], [-10000, -10000], [-10000, -10000], [-10000, -10000],
                                    [10000, 10000], [10000, 10000], [10000, 10000], [10000, 10000]]
  SQUARE_WAVE_CYCLE[:stereo][32] = [[-1000000000, -1000000000], [-1000000000, -1000000000],
                                    [-1000000000, -1000000000], [-1000000000, -1000000000],
                                    [ 1000000000,  1000000000], [ 1000000000,  1000000000],
                                    [ 1000000000,  1000000000], [ 1000000000,  1000000000]]


  def setup
    clean_output_folder()
  end

  def test_write_file_with_no_sample_data
    writer = Writer.new("#{OUTPUT_FOLDER}/no_samples.wav", Format.new(1, 8, 44100))
    writer.close()
    
    assert_equal(read_file(:expected, "no_samples.wav"), read_file(:actual, "no_samples.wav"))
  end

  def test_write_basic_file
    [:mono, :stereo].each do |channels|
      Format::SUPPORTED_BITS_PER_SAMPLE.each do |bits_per_sample|
        file_name = "valid_#{channels}_#{bits_per_sample}_44100.wav"
        format = Format.new(channels, bits_per_sample, 44100)

        writer = Writer.new("#{OUTPUT_FOLDER}/#{file_name}", format)
        writer.write(Buffer.new(SQUARE_WAVE_CYCLE[channels][bits_per_sample] * 128, format))
        writer.write(Buffer.new(SQUARE_WAVE_CYCLE[channels][bits_per_sample] * 128, format))
        writer.write(Buffer.new(SQUARE_WAVE_CYCLE[channels][bits_per_sample] * 24, format))
        writer.close()

        assert_equal(read_file(:expected, file_name), read_file(:actual, file_name))
      end
    end
  end

  def test_write_basic_file_with_a_block
    file_name = "valid_mono_16_44100.wav"
    format = Format.new(1, 16, 44100)

    writer = Writer.new("#{OUTPUT_FOLDER}/#{file_name}", format) do |writer|
      4.times do
        writer.write(Buffer.new(SQUARE_WAVE_CYCLE[:mono][16] * 70, format))
      end
    end

    assert_equal(read_file(:expected, file_name), read_file(:actual, file_name))
    assert(writer.closed?)
  end

  def test_write_buffers_of_different_formats
    file_name = "valid_mono_8_44100.wav"
    format_8bit_mono    = Format.new(:mono,   8,  44100)
    format_16_bit_mono  = Format.new(:mono,   16, 22050)
    format_16bit_stereo = Format.new(:stereo, 16, 44100)

    writer = Writer.new("#{OUTPUT_FOLDER}/#{file_name}", format_8bit_mono)
    writer.write(Buffer.new(SQUARE_WAVE_CYCLE[:stereo][16] * 128, format_16bit_stereo))
    writer.write(Buffer.new(SQUARE_WAVE_CYCLE[:mono][16] * 128,   format_16_bit_mono))
    writer.write(Buffer.new(SQUARE_WAVE_CYCLE[:stereo][16] * 24,  format_16bit_stereo))
    writer.close()

    assert_equal(read_file(:expected, file_name), read_file(:actual, file_name))
  end

  def test_write_file_with_padding_byte
    file_name = "valid_mono_8_44100_with_padding_byte.wav"
    format = Format.new(1, 8, 44100)

    writer = Writer.new("#{OUTPUT_FOLDER}/#{file_name}", format)
    writer.write(Buffer.new(SQUARE_WAVE_CYCLE[:mono][8] * 128, format))
    writer.write(Buffer.new(SQUARE_WAVE_CYCLE[:mono][8] * 128, format))
    writer.write(Buffer.new(SQUARE_WAVE_CYCLE[:mono][8] * 23 + [88, 88, 88, 88, 167, 167, 167], format))
    writer.close()

    assert_equal(read_file(:expected, file_name), read_file(:actual, file_name))
  end<commit_msg>Updating Writer.write() tests to test different channels/bits per sample combinations when a block is used.<commit_after>$:.unshift File.join(File.dirname(__FILE__),'..','lib')

require 'test/unit'
require 'wavefile.rb'

include WaveFile

class WriterTest < Test::Unit::TestCase
  OUTPUT_FOLDER = "test/fixtures/actual_output"

  SQUARE_WAVE_CYCLE = {}
  SQUARE_WAVE_CYCLE[:mono] = {}
  SQUARE_WAVE_CYCLE[:mono][8] =    [88, 88, 88, 88, 167, 167, 167, 167]
  SQUARE_WAVE_CYCLE[:mono][16] =   [-10000, -10000, -10000, -10000, 10000, 10000, 10000, 10000]
  SQUARE_WAVE_CYCLE[:mono][32] =   [-1000000000, -1000000000, -1000000000, -1000000000,
                                     1000000000, 1000000000, 1000000000, 1000000000]
  SQUARE_WAVE_CYCLE[:stereo] = {}
  SQUARE_WAVE_CYCLE[:stereo][8] =  [[88, 88], [88, 88], [88, 88], [88, 88],
                                    [167, 167], [167, 167], [167, 167], [167, 167]]
  SQUARE_WAVE_CYCLE[:stereo][16] = [[-10000, -10000], [-10000, -10000], [-10000, -10000], [-10000, -10000],
                                    [10000, 10000], [10000, 10000], [10000, 10000], [10000, 10000]]
  SQUARE_WAVE_CYCLE[:stereo][32] = [[-1000000000, -1000000000], [-1000000000, -1000000000],
                                    [-1000000000, -1000000000], [-1000000000, -1000000000],
                                    [ 1000000000,  1000000000], [ 1000000000,  1000000000],
                                    [ 1000000000,  1000000000], [ 1000000000,  1000000000]]


  def setup
    clean_output_folder()
  end

  def test_write_file_with_no_sample_data
    writer = Writer.new("#{OUTPUT_FOLDER}/no_samples.wav", Format.new(1, 8, 44100))
    writer.close()
    
    assert_equal(read_file(:expected, "no_samples.wav"), read_file(:actual, "no_samples.wav"))
  end

  def test_write_basic_file
    [:mono, :stereo].each do |channels|
      Format::SUPPORTED_BITS_PER_SAMPLE.each do |bits_per_sample|
        file_name = "valid_#{channels}_#{bits_per_sample}_44100.wav"
        format = Format.new(channels, bits_per_sample, 44100)

        writer = Writer.new("#{OUTPUT_FOLDER}/#{file_name}", format)
        writer.write(Buffer.new(SQUARE_WAVE_CYCLE[channels][bits_per_sample] * 128, format))
        writer.write(Buffer.new(SQUARE_WAVE_CYCLE[channels][bits_per_sample] * 128, format))
        writer.write(Buffer.new(SQUARE_WAVE_CYCLE[channels][bits_per_sample] * 24, format))
        writer.close()

        assert_equal(read_file(:expected, file_name), read_file(:actual, file_name))
      end
    end
  end

  def test_write_basic_file_with_a_block
    [:mono, :stereo].each do |channels|
      Format::SUPPORTED_BITS_PER_SAMPLE.each do |bits_per_sample|
        file_name = "valid_#{channels}_#{bits_per_sample}_44100.wav"
        format = Format.new(channels, bits_per_sample, 44100)

        writer = Writer.new("#{OUTPUT_FOLDER}/#{file_name}", format) do |writer|
          4.times do
            writer.write(Buffer.new(SQUARE_WAVE_CYCLE[channels][bits_per_sample] * 70, format))
          end
        end

        assert_equal(read_file(:expected, file_name), read_file(:actual, file_name))
        assert(writer.closed?)
      end
    end
  end

  def test_write_buffers_of_different_formats
    file_name = "valid_mono_8_44100.wav"
    format_8bit_mono    = Format.new(:mono,   8,  44100)
    format_16_bit_mono  = Format.new(:mono,   16, 22050)
    format_16bit_stereo = Format.new(:stereo, 16, 44100)

    writer = Writer.new("#{OUTPUT_FOLDER}/#{file_name}", format_8bit_mono)
    writer.write(Buffer.new(SQUARE_WAVE_CYCLE[:stereo][16] * 128, format_16bit_stereo))
    writer.write(Buffer.new(SQUARE_WAVE_CYCLE[:mono][16] * 128,   format_16_bit_mono))
    writer.write(Buffer.new(SQUARE_WAVE_CYCLE[:stereo][16] * 24,  format_16bit_stereo))
    writer.close()

    assert_equal(read_file(:expected, file_name), read_file(:actual, file_name))
  end

  def test_write_file_with_padding_byte
    file_name = "valid_mono_8_44100_with_padding_byte.wav"
    format = Format.new(1, 8, 44100)

    writer = Writer.new("#{OUTPUT_FOLDER}/#{file_name}", format)
    writer.write(Buffer.new(SQUARE_WAVE_CYCLE[:mono][8] * 128, format))
    writer.write(Buffer.new(SQUARE_WAVE_CYCLE[:mono][8] * 128, format))
    writer.write(Buffer.new(SQUARE_WAVE_CYCLE[:mono][8] * 23 + [88, 88, 88, 88, 167, 167, 167], format))
    writer.close()

    assert_equal(read_file(:expected, file_name), read_file(:actual, file_name))
  end
{'fraction_non_alphanumeric': 0.1429237041351194, 'fraction_numerical': 0.13150844496214328, 'mean_word_length': 3.593900481540931, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 48, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7016361', 'n_tokens_mistral': 4037, 'n_tokens_neox': 3100, 'n_words': 500}