UnitConverterTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. class HTMLPurifier_UnitConverterTest extends HTMLPurifier_Harness
  3. {
  4. protected function assertConversion($input, $expect, $unit = null, $test_negative = true) {
  5. $length = HTMLPurifier_Length::make($input);
  6. if ($expect !== false) $expectl = HTMLPurifier_Length::make($expect);
  7. else $expectl = false;
  8. $to_unit = $unit !== null ? $unit : $expectl->getUnit();
  9. $converter = new HTMLPurifier_UnitConverter(4, 10);
  10. $result = $converter->convert($length, $to_unit);
  11. if (!$result || !$expectl) $this->assertIdentical($result, $expectl);
  12. else $this->assertIdentical($result->toString(), $expectl->toString());
  13. $converter = new HTMLPurifier_UnitConverter(4, 10, true);
  14. $result = $converter->convert($length, $to_unit);
  15. if (!$result || !$expectl) $this->assertIdentical($result, $expectl);
  16. else $this->assertIdentical($result->toString(), $expectl->toString(), 'BCMath substitute: %s');
  17. if ($test_negative) {
  18. $this->assertConversion(
  19. "-$input",
  20. $expect === false ? false : "-$expect",
  21. $unit,
  22. false
  23. );
  24. }
  25. }
  26. function testFail() {
  27. $this->assertConversion('1in', false, 'foo');
  28. $this->assertConversion('1foo', false, 'in');
  29. }
  30. function testZero() {
  31. $this->assertConversion('0', '0', 'in', false);
  32. $this->assertConversion('-0', '0', 'in', false);
  33. $this->assertConversion('0in', '0', 'in', false);
  34. $this->assertConversion('-0in', '0', 'in', false);
  35. $this->assertConversion('0in', '0', 'pt', false);
  36. $this->assertConversion('-0in', '0', 'pt', false);
  37. }
  38. function testEnglish() {
  39. $this->assertConversion('1in', '6pc');
  40. $this->assertConversion('6pc', '1in');
  41. $this->assertConversion('1in', '72pt');
  42. $this->assertConversion('72pt', '1in');
  43. $this->assertConversion('1pc', '12pt');
  44. $this->assertConversion('12pt', '1pc');
  45. $this->assertConversion('1pt', '0.01389in');
  46. $this->assertConversion('1.000pt', '0.01389in');
  47. $this->assertConversion('100000pt', '1389in');
  48. $this->assertConversion('1in', '96px');
  49. $this->assertConversion('96px', '1in');
  50. }
  51. function testMetric() {
  52. $this->assertConversion('1cm', '10mm');
  53. $this->assertConversion('10mm', '1cm');
  54. $this->assertConversion('1mm', '0.1cm');
  55. $this->assertConversion('100mm', '10cm');
  56. }
  57. function testEnglishMetric() {
  58. $this->assertConversion('2.835pt', '1mm');
  59. $this->assertConversion('1mm', '2.835pt');
  60. $this->assertConversion('0.3937in', '1cm');
  61. }
  62. function testRoundingMinPrecision() {
  63. // One sig-fig, modified to be four, conversion rounds up
  64. $this->assertConversion('100pt', '1.389in');
  65. $this->assertConversion('1000pt', '13.89in');
  66. $this->assertConversion('10000pt', '138.9in');
  67. $this->assertConversion('100000pt', '1389in');
  68. $this->assertConversion('1000000pt', '13890in');
  69. }
  70. function testRoundingUserPrecision() {
  71. // Five sig-figs, conversion rounds down
  72. $this->assertConversion('11112000pt', '154330in');
  73. $this->assertConversion('1111200pt', '15433in');
  74. $this->assertConversion('111120pt', '1543.3in');
  75. $this->assertConversion('11112pt', '154.33in');
  76. $this->assertConversion('1111.2pt', '15.433in');
  77. $this->assertConversion('111.12pt', '1.5433in');
  78. $this->assertConversion('11.112pt', '0.15433in');
  79. }
  80. function testRoundingBigNumber() {
  81. $this->assertConversion('444400000000000000000000in', '42660000000000000000000000px');
  82. }
  83. protected function assertSigFig($n, $sigfigs) {
  84. $converter = new HTMLPurifier_UnitConverter();
  85. $result = $converter->getSigFigs($n);
  86. $this->assertIdentical($result, $sigfigs);
  87. }
  88. function test_getSigFigs() {
  89. $this->assertSigFig('0', 0);
  90. $this->assertSigFig('1', 1);
  91. $this->assertSigFig('-1', 1);
  92. $this->assertSigFig('+1', 1);
  93. $this->assertSigFig('01', 1);
  94. $this->assertSigFig('001', 1);
  95. $this->assertSigFig('12', 2);
  96. $this->assertSigFig('012', 2);
  97. $this->assertSigFig('10', 1);
  98. $this->assertSigFig('10.', 2);
  99. $this->assertSigFig('100.', 3);
  100. $this->assertSigFig('103', 3);
  101. $this->assertSigFig('130', 2);
  102. $this->assertSigFig('.1', 1);
  103. $this->assertSigFig('0.1', 1);
  104. $this->assertSigFig('00.1', 1);
  105. $this->assertSigFig('0.01', 1);
  106. $this->assertSigFig('0.010', 2);
  107. $this->assertSigFig('0.012', 2);
  108. }
  109. }
  110. // vim: et sw=4 sts=4