PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/test/slow/collection_classes/deep_copy.php

http://github.com/facebook/hiphop-php
PHP | 53 lines | 32 code | 14 blank | 7 comment | 2 complexity | dcf8f1c5274b23fbe3005c90abd14e01 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. <?hh
  2. // Test the deep copy mechanism that is triggered
  3. // when collection literals are used as initializers
  4. // for class properties.
  5. class A {
  6. public $fv = ImmVector {ImmVector {1, 2}, 3};
  7. public $v = Vector {Vector {1, 2}, 3};
  8. public $s = Set {1, 2, 3};
  9. public $m = Map {0 => Map{0 => 1}, 1 => 2, 2 => 3};
  10. public $p = Pair {Pair{1, 2}, 3};
  11. }
  12. function main() {
  13. $a1 = new A();
  14. $a2 = new A();
  15. // Check that every instance gets their own copy
  16. // of the collection literal.
  17. echo "\nVector...\n";
  18. $a1->v[0][] = Vector {1};
  19. print_r($a1->v);
  20. print_r($a2->v);
  21. echo "\nSet...\n";
  22. $a1->s->add(4);
  23. var_dump($a1->s == $a2->s);
  24. echo "\nMap...\n";
  25. $a1->m[3] = 4;
  26. var_dump($a1->m == $a2->m);
  27. // Pair and ImmVector are immutable, so
  28. // we can't do a similar test for them.
  29. echo "\nPair...\n";
  30. print_r($a1->p);
  31. print_r($a2->p);
  32. echo "\nImmVector...\n";
  33. print_r($a1->fv);
  34. print_r($a2->fv);
  35. }
  36. <<__EntryPoint>>
  37. function main_deep_copy() {
  38. main();
  39. }