<?php
namespace nur;

use nur\impl\GBaseArray;
use nur\impl\GIteratableArray;
use nur\t\TestCase;
use stdClass;

class ATest extends TestCase {
  function testEnsureAccess() {
    $o = null;
    $a =& A::ensure_access($o);
    self::assertSame($a, $o);
    self::assertSame([], $a);

    $o = false;
    $a =& A::ensure_access($o);
    self::assertSame($a, $o);
    self::assertSame([], $a);

    $o = [];
    $a =& A::ensure_access($o);
    self::assertSame($a, $o);
    self::assertSame([], $a);

    $o = "any";
    $a =& A::ensure_access($o);
    self::assertSame($a, $o);
    self::assertSame(["any"], $a);

    $o = 123;
    $a =& A::ensure_access($o);
    self::assertSame($a, $o);
    self::assertSame([123], $a);

    $x = new stdClass();
    $o = $x;
    $a =& A::ensure_access($o);
    self::assertSame($a, $o);
    self::assertSame([$x], $a);

    $o = new GBaseArray([]);
    $a =& A::ensure_access($o);
    self::assertNotSame($a, $o);
    self::assertIsArray($a);
    $a[] = "hello";
    $a[] = "world";
    self::assertSame(["hello", "world"], $o->array());

    $o = new GIteratableArray([]);
    $a =& A::ensure_access($o);
    self::assertNotSame($a, $o);
    self::assertIsArray($a);
    $a[] = "hello";
    $a[] = "world";
    self::assertSame(["hello", "world"], $o->array());
  }

  function testEnsure_size() {
    $template = null;
    $array = $template; A::ensure_size($array, 0);
    self::assertSame([], $array);
    $array = $template; A::ensure_size($array, 1);
    self::assertSame([null], $array);
    $array = $template; A::ensure_size($array, 2);
    self::assertSame([null, null], $array);

    $template = [];
    $array = $template; A::ensure_size($array, 0);
    self::assertSame([], $array);
    $array = $template; A::ensure_size($array, 1);
    self::assertSame([null], $array);
    $array = $template; A::ensure_size($array, 2);
    self::assertSame([null, null], $array);

    $template = [0, 1, "a" => "b", "c" => "d"];
    $array = $template; A::ensure_size($array, 0);
    self::assertSame([], $array);
    $array = $template; A::ensure_size($array, 1);
    self::assertSame([0], $array);
    $array = $template; A::ensure_size($array, 2);
    self::assertSame([0, 1], $array);
    $array = $template; A::ensure_size($array, 3);
    self::assertSame([0, 1, "a" => "b"], $array);
    $array = $template; A::ensure_size($array, 4);
    self::assertSame([0, 1, "a" => "b", "c" => "d"], $array);
    $array = $template; A::ensure_size($array, 5);
    self::assertSame([0, 1, "a" => "b", "c" => "d", null], $array);
  }

  const A = [
    0 => "1st", 1 => "2nd",
    10 => "a", 15 => "b", "x" => "y",
    "dn" => null, "df" => false,
  ];
  const B = [
    0 => "3rd", 1 => "4th",
    15 => "c", 20 => "d", "x" => "z",
    "sn" => null, "sf" => false,
    "dn" => "w", "df" => "t",
  ];

  const MERGE = [
    0 => "1st", 1 => "2nd",
    2 => "a", 3 => "b", "x" => "z",
    "dn" => "w", "df" => "t",
    4 => "3rd", 5 => "4th",
    6 => "c", 7 => "d",
    "sn" => null, "sf" => false,
  ];
  function testMerge() {
    $a = self::A; $b = self::B;
    A::merge($a, $b);
    self::assertSame(self::MERGE, $a);
  }

  const MERGE2 = [
    0 => "3rd", 1 => "4th",
    10 => "a", 15 => "c", "x" => "z",
    "dn" => "w", "df" => "t",
    20 => "d",
    "sn" => null, "sf" => false,
  ];
  function testMerge2() {
    $a = self::A; $b = self::B;
    A::merge2($a, $b);
    self::assertSame(self::MERGE2, $a);
  }

  const MERGE3 = [
    0 => "1st", 1 => "2nd",
    10 => "a", 15 => "c", "x" => "z",
    "dn" => "w", "df" => "t",
    2 => "3rd", 3 => "4th",
    20 => "d",
    "sn" => null, "sf" => false,
  ];
  function testMerge3() {
    $a = self::A; $b = self::B;
    A::merge3($a, $b);
    self::assertSame(self::MERGE3, $a);
  }

  const MERGE_NN = [
    0 => "1st", 1 => "2nd",
    2 => "a", 3 => "b", "x" => "z",
    "dn" => "w", "df" => "t",
    4 => "3rd", 5 => "4th",
    6 => "c", 7 => "d",
    "sf" => false,
  ];
  function testMerge_nn() {
    $a = self::A; $b = self::B;
    A::merge_nn($a, $b);
    self::assertSame(self::MERGE_NN, $a);
  }

  const MERGE_NN2 = [
    0 => "3rd", 1 => "4th",
    10 => "a", 15 => "c", "x" => "z",
    "dn" => "w", "df" => "t",
    20 => "d",
    "sf" => false,
  ];
  function testMerge_nn2() {
    $a = self::A; $b = self::B;
    A::merge_nn2($a, $b);
    self::assertSame(self::MERGE_NN2, $a);
  }

  const MERGE_NZ = [
    0 => "1st", 1 => "2nd",
    2 => "a", 3 => "b", "x" => "z",
    "dn" => "w", "df" => "t",
    4 => "3rd", 5 => "4th",
    6 => "c", 7 => "d",
  ];
  function testMerge_nz() {
    $a = self::A; $b = self::B;
    A::merge_nz($a, $b);
    self::assertSame(self::MERGE_NZ, $a);
  }

  const MERGE_NZ2 = [
    0 => "3rd", 1 => "4th",
    10 => "a", 15 => "c", "x" => "z",
    "dn" => "w", "df" => "t",
    20 => "d",
  ];
  function testMerge_nz2() {
    $a = self::A; $b = self::B;
    A::merge_nz2($a, $b);
    self::assertSame(self::MERGE_NZ2, $a);
  }

  const UPDATE_NX = [
    0 => "1st", 1 => "2nd",
    2 => "a", 3 => "b", "x" => "y",
    "dn" => null, "df" => false,
    4 => "d",
    "sn" => null, "sf" => false,
  ];
  function testUpdate_nx() {
    $a = self::A; $b = self::B;
    A::update_nx($a, $b);
    self::assertSame(self::UPDATE_NX, $a);
  }

  const UPDATE_NX2 = [
    0 => "1st", 1 => "2nd",
    10 => "a", 15 => "b", "x" => "y",
    "dn" => null, "df" => false,
    20 => "d",
    "sn" => null, "sf" => false,
  ];
  function testUpdate_nx2() {
    $a = self::A; $b = self::B;
    A::update_nx2($a, $b);
    self::assertSame(self::UPDATE_NX2, $a);
  }

  const UPDATE_N = [
    0 => "1st", 1 => "2nd",
    2 => "a", 3 => "b", "x" => "y",
    "dn" => "w", "df" => false,
    4 => "d",
    "sn" => null, "sf" => false,
  ];
  function testUpdate_n() {
    $a = self::A; $b = self::B;
    A::update_n($a, $b);
    self::assertSame(self::UPDATE_N, $a);
  }

  const UPDATE_N2 = [
    0 => "1st", 1 => "2nd",
    10 => "a", 15 => "b", "x" => "y",
    "dn" => "w", "df" => false,
    20 => "d",
    "sn" => null, "sf" => false,
  ];
  function testUpdate_n2() {
    $a = self::A; $b = self::B;
    A::update_n2($a, $b);
    self::assertSame(self::UPDATE_N2, $a);
  }

  const UPDATE_Z = [
    0 => "1st", 1 => "2nd",
    2 => "a", 3 => "b", "x" => "y",
    "dn" => "w", "df" => "t",
    4 => "d",
    "sn" => null, "sf" => false,
  ];
  function testUpdate_z() {
    $a = self::A; $b = self::B;
    A::update_z($a, $b);
    self::assertSame(self::UPDATE_Z, $a);
  }

  const UPDATE_Z2 = [
    0 => "1st", 1 => "2nd",
    10 => "a", 15 => "b", "x" => "y",
    "dn" => "w", "df" => "t",
    20 => "d",
    "sn" => null, "sf" => false,
  ];
  function testUpdate_z2() {
    $a = self::A; $b = self::B;
    A::update_z2($a, $b);
    self::assertSame(self::UPDATE_Z2, $a);
  }

  function testFlatten() {
    $array = null; A::flatten($array);
    self::assertSame([], $array);

    $array = []; A::flatten($array);
    self::assertSame([], $array);

    $array = ["a", "b", "c" => 1, "d"]; A::flatten($array);
    self::assertSame(["a", "b", "c" => 1, "d"], $array);

    $array = ["a", "b", ["c" => 1, "d"]]; A::flatten($array);
    self::assertSame(["a", "b", "c" => 1, "d"], $array);

    $array = ["a", "b", ["c" => 1, "d"], ["c" => 2]]; A::flatten($array);
    self::assertSame(["a", "b", "c" => [1, 2], "d"], $array);

    $array = [0, null, false, "a" => 0, ["a" => null], ["a" => false]]; A::flatten($array);
    self::assertSame([0, null, false, "a" => 0], $array);
  }

  function testExtract_seq() {
    self::assertSame(null, A::extract_seq(null));
    self::assertSame(null, A::extract_seq([]));
    self::assertSame(null, A::extract_seq([1 => "a"]));
    self::assertSame(["b"], A::extract_seq([1 => "a", 0 => "b"]));
    self::assertSame(["a"], A::extract_seq(["a"]));
    self::assertSame(["a", "b"], A::extract_seq(["a", "b"]));
    self::assertSame(["a"], A::extract_seq(["a", "b" => "c"]));
    self::assertSame(["c"], A::extract_seq(["a" => "b", "c"]));
  }

  function testExtract_assoc() {
    self::assertSame(null, A::extract_assoc(null));
    self::assertSame(null, A::extract_assoc([]));
    self::assertSame([1 => "a"], A::extract_assoc([1 => "a"]));
    self::assertSame([1 => "a"], A::extract_assoc([1 => "a", 0 => "b"]));
    self::assertSame(null, A::extract_assoc(["a"]));
    self::assertSame(null, A::extract_assoc(["a", "b"]));
    self::assertSame(["b" => "c"], A::extract_assoc(["a", "b" => "c"]));
    self::assertSame(["a" => "b"], A::extract_assoc(["a" => "b", "c"]));
  }

  function testSelect_replace() {
    $src = null;
    self::assertSame([], A::select_replace2($src, null));
    self::assertSame([], A::select_replace2($src, []));
    self::assertSame(["a" => null], A::select_replace2($src, ["a"]));

    $src = [];
    self::assertSame([], A::select_replace2($src, null));
    self::assertSame([], A::select_replace2($src, []));
    self::assertSame(["a" => null], A::select_replace2($src, ["a"]));

    $src = ["a" => 1, "b" => 2];
    self::assertSame(["a" => 1, "b" => 2], A::select_replace2($src, null));
    self::assertSame([], A::select_replace2($src, []));
    self::assertSame(["a" => 1], A::select_replace2($src, ["a"]));

    $src = ["a" => 1, "b" => 2, "c" => 3];
    self::assertSame(
      ["a" => 1, "x" => 9, "z" => 8, "c" => 3, "d" => null],
      A::select_replace2($src, ["a", "x" => 9, ["z" => 8, "c"], "d"]));
  }
}
