<commit_before>defmodule SMPPEX.Protocol.OptionalFieldsBuilderTest do
  use ExUnit.Case

  import SMPPEX.Protocol.OptionalFieldsBuilder

  defp flat_bin({:ok, io_list}) do
    io_list |> List.flatten |> Enum.join
  end

  test "build unknown valid" do
    res = build(%{0x01 => "val"})

    assert {:ok, _} = res
    assert <<0, 0x01, 0, 0x03, ?v, ?a, ?l>> == flat_bin(res)
  end

  test "build unknown invalid" do
    big_bin = [0] |> Stream.cycle |> Enum.take(65536) |> to_string
    res = build(%{0x01 => big_bin})

    assert {:error, _} = res

    res = build(%{-0x01 => "test"})

    assert {:error, _} = res
  end

  test "build known valid: integer" do
    res = build(%{0x05 => 123})

    assert {:ok, _} = res
    assert <<0, 0x05, 0, 0x01, 123>> == flat_bin(res)
  end


  test "build known valid: c_octet_string" do
    res = build(%{0x1D => "test"})

    assert {:ok, _} = res
    assert <<0, 0x1D, 0, 0x05, "test", 0>> == flat_bin(res)
  end

  test "build known valid: octet_string" do
    res = build(%{0x0381 => "test"})

    assert {:ok, _} = res
    assert <<0x03, 0x81, 0, 0x04, "test">> == flat_bin(res)
  end
<commit_msg>more tests for tlv builder<commit_after>defmodule SMPPEX.Protocol.OptionalFieldsBuilderTest do
  use ExUnit.Case

  import SMPPEX.Protocol.OptionalFieldsBuilder

  defp flat_bin({:ok, io_list}) do
    io_list |> List.flatten |> Enum.join
  end

  test "build unknown valid" do
    res = build(%{0x01 => "val"})
    assert {:ok, _} = res
    assert <<0, 0x01, 0, 0x03, ?v, ?a, ?l>> == flat_bin(res)
  end

  test "build unknown invalid" do
    big_bin = [0] |> Stream.cycle |> Enum.take(65536) |> to_string
    res = build(%{0x01 => big_bin})
    assert {:error, _} = res

    res = build(%{-0x01 => "test"})
    assert {:error, _} = res
  end

  test "build known valid: integer" do
    res = build(%{0x05 => 123})

    assert {:ok, _} = res
    assert <<0, 0x05, 0, 0x01, 123>> == flat_bin(res)
  end

  test "build known valid: c_octet_string" do
    res = build(%{0x1D => "test"})
    assert {:ok, _} = res
    assert <<0, 0x1D, 0, 0x05, "test", 0>> == flat_bin(res)
  end

  test "build known valid: octet_string" do
    res = build(%{0x0381 => "test"})
    assert {:ok, _} = res
    assert <<0x03, 0x81, 0, 0x04, "test">> == flat_bin(res)
  end

  test "build known invalid: integer" do
    res = build(%{0x05 => "sdf"})
    assert {:error, _} = res

    res = build(%{0x05 => -1})
    assert {:error, _} = res

    res = build(%{0x05 => 256})
    assert {:error, _} = res
  end

  test "build known invalid: c_octet_string" do
    res = build(%{0x1D => 123})
    assert {:error, _} = res

    s = <<1>> |> List.duplicate(65) |> Enum.join
    res = build(%{0x1E => << s :: binary, 0 >>})
    assert {:error, _} = res
  end

  test "build known invalid: octet_string" do
    res = build(%{0x0423 => "test"})
    assert {:error, _} = res
  end

{'fraction_non_alphanumeric': 0.17093425605536333, 'fraction_numerical': 0.05501730103806229, 'mean_word_length': 2.9986168741355463, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 53, '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': '6030164', 'n_tokens_mistral': 1253, 'n_tokens_neox': 1109, 'n_words': 307}