<commit_before># frozen_string_literal: true

require "gir_ffi_test_helper"

describe GirFFI::SizedArray do
  describe "::wrap" do
    it "takes a type, size and pointer and returns a GirFFI::SizedArray wrapping them" do
      expect(ptr = Object.new).to receive(:null?).and_return false
      sarr = GirFFI::SizedArray.wrap :gint32, 3, ptr
      assert_instance_of GirFFI::SizedArray, sarr
      assert_equal ptr, sarr.to_ptr
      assert_equal 3, sarr.size
      assert_equal :gint32, sarr.element_type
    end

    it "returns nil if the wrapped pointer is null" do
      expect(ptr = Object.new).to receive(:null?).and_return true
      sarr = GirFFI::SizedArray.wrap :gint32, 3, ptr
      _(sarr).must_be_nil
    end
  end

  describe "#each" do
    it "yields each element" do
      ary = %w(one two three)
      ptrs = ary.map { |a| FFI::MemoryPointer.from_string(a) }
      ptrs << nil
      block = FFI::MemoryPointer.new(:pointer, ptrs.length)
      block.write_array_of_pointer ptrs

      sarr = GirFFI::SizedArray.new :utf8, 3, block
      arr = []
      sarr.each do |str|
        arr << str
      end
      assert_equal %w(one two three), arr
    end
  end

  describe "::from" do
    describe "from a Ruby array" do
      it "creates a GirFFI::SizedArray with the same elements" do
        arr = GirFFI::SizedArray.from :gint32, 3, [3, 2, 1]
        _(arr).must_be_instance_of GirFFI::SizedArray
        assert_equal [3, 2, 1], arr.to_a
      end

      it "raises an error if the array has the wrong number of elements" do
        _(proc { GirFFI::SizedArray.from :gint32, 4, [3, 2, 1] }).must_raise ArgumentError
      end

      it "uses the array's size if passed -1 as the size" do
        arr = GirFFI::SizedArray.from :gint32, -1, [3, 2, 1]
        _(arr.size).must_equal 3
      end
    end

    describe "from a Ruby string" do
      it "creates a GirFFI::SizedArray with elements corresponding to the string's bytes" do
        arr = GirFFI::SizedArray.from :guint8, 3, "foo"
        _(arr).must_be_instance_of GirFFI::SizedArray
        assert_equal "foo".bytes, arr.to_a
      end
    end

    describe "from a GirFFI::SizedArray" do
      it "return its argument" do
        arr = GirFFI::SizedArray.from :gint32, 3, [3, 2, 1]
        arr2 = GirFFI::SizedArray.from :gint32, 3, arr
        assert_equal arr, arr2<commit_msg>Add passing test for creating a SizedArray of strings<commit_after># frozen_string_literal: true

require "gir_ffi_test_helper"

describe GirFFI::SizedArray do
  describe "::wrap" do
    it "takes a type, size and pointer and returns a GirFFI::SizedArray wrapping them" do
      expect(ptr = Object.new).to receive(:null?).and_return false
      sarr = GirFFI::SizedArray.wrap :gint32, 3, ptr
      assert_instance_of GirFFI::SizedArray, sarr
      assert_equal ptr, sarr.to_ptr
      assert_equal 3, sarr.size
      assert_equal :gint32, sarr.element_type
    end

    it "returns nil if the wrapped pointer is null" do
      expect(ptr = Object.new).to receive(:null?).and_return true
      sarr = GirFFI::SizedArray.wrap :gint32, 3, ptr
      _(sarr).must_be_nil
    end
  end

  describe "#each" do
    it "yields each element" do
      ary = %w(one two three)
      ptrs = ary.map { |a| FFI::MemoryPointer.from_string(a) }
      ptrs << nil
      block = FFI::MemoryPointer.new(:pointer, ptrs.length)
      block.write_array_of_pointer ptrs

      sarr = GirFFI::SizedArray.new :utf8, 3, block
      arr = []
      sarr.each do |str|
        arr << str
      end
      assert_equal %w(one two three), arr
    end
  end

  describe "::from" do
    describe "from a Ruby array" do
      it "creates a GirFFI::SizedArray with the same elements" do
        arr = GirFFI::SizedArray.from :gint32, 3, [3, 2, 1]
        _(arr).must_be_instance_of GirFFI::SizedArray
        assert_equal [3, 2, 1], arr.to_a
      end

      it "raises an error if the array has the wrong number of elements" do
        _(proc { GirFFI::SizedArray.from :gint32, 4, [3, 2, 1] }).must_raise ArgumentError
      end

      it "uses the array's size if passed -1 as the size" do
        arr = GirFFI::SizedArray.from :gint32, -1, [3, 2, 1]
        _(arr.size).must_equal 3
      end

      it "can create an array of strings" do
        arr = GirFFI::SizedArray.from :utf8, 3, ["foo", "bar", "baz"]
        _(arr).must_be_instance_of GirFFI::SizedArray
        _(arr.to_a).must_equal ["foo", "bar", "baz"]
      end
    end

    describe "from a Ruby string" do
      it "creates a GirFFI::SizedArray with elements corresponding to the string's bytes" do
        arr = GirFFI::SizedArray.from :guint8, 3, "foo"
        _(arr).must_be_instance_of GirFFI::SizedArray
        assert_equal "foo".bytes, arr.to_a
      end
    end

    describe "from a GirFFI::SizedArray" do
      it "return its argument" do
        arr = GirFFI::SizedArray.from :gint32, 3, [3, 2, 1]
        arr2 = GirFFI::SizedArray.from :gint32, 3, arr
        assert_equal arr, arr2
{'fraction_non_alphanumeric': 0.10323358104036955, 'fraction_numerical': 0.019280980116489255, 'mean_word_length': 2.7026022304832713, 'pattern_counts': {'":': 4, '<': 11, '<?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': '4130442', 'n_tokens_mistral': 1796, 'n_tokens_neox': 1703, 'n_words': 521}