ILIAS  trunk Revision v11.0_alpha-1753-gb21ca8c4367
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
RangeTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 
27 class RangeTest extends TestCase
28 {
29  public function testFactory(): Range
30  {
31  $f = new ILIAS\Data\Factory();
32  $range = $f->range(1, 2);
33  $this->assertInstanceOf(Range::class, $range);
34  return $range;
35  }
36 
40  public function testValues(Range $range): void
41  {
42  $this->assertEquals(1, $range->getStart());
43  $this->assertEquals(2, $range->getLength());
44  }
45 
49  public function testEndCalculation(Range $range): void
50  {
51  $this->assertEquals(3, $range->getEnd());
52  }
53 
57  public function testWithLength(Range $range): Range
58  {
59  $range = $range->withLength(3);
60  $this->assertEquals(1, $range->getStart());
61  $this->assertEquals(3, $range->getLength());
62  $this->assertEquals(4, $range->getEnd());
63  return $range;
64  }
65 
69  public function testWithStart(Range $range): Range
70  {
71  $range = $range->withStart(3);
72  $this->assertEquals(3, $range->getStart());
73  $this->assertEquals(3, $range->getLength());
74  $this->assertEquals(6, $range->getEnd());
75  return $range;
76  }
77 
81  public function testUnpack(Range $range): void
82  {
83  $this->assertEquals(
84  [3,3],
85  $range->unpack()
86  );
87  }
88 
92  public function testNegativeStart(Range $range): void
93  {
94  $this->expectException(InvalidArgumentException::class);
95  $range = $range->withStart(-5);
96  }
97 
101  public function testNegativeLength(Range $range): void
102  {
103  $this->expectException(InvalidArgumentException::class);
104  $range = $range->withLength(-1);
105  }
106 
107  public function testConstructionWrongStart(): void
108  {
109  $this->expectException(InvalidArgumentException::class);
110  $f = new ILIAS\Data\Factory();
111  $range = $f->range(-1, 2);
112  }
113 
114  public function testConstructionWrongLength(): void
115  {
116  $this->expectException(InvalidArgumentException::class);
117  $f = new ILIAS\Data\Factory();
118  $range = $f->range(1, -2);
119  }
120 
124  public function testCroppedTo($start, $length, $max, $has_changed): void
125  {
126  $f = new ILIAS\Data\Factory();
127  $range = $f->range($start, $length);
128  $cropped = $range->croppedTo($max);
129 
130  if (!$has_changed) {
131  $this->assertEquals($range, $cropped);
132  } else {
133  $this->assertEquals(min($max, $start), $cropped->getStart());
134  $this->assertEquals($max, $cropped->getEnd());
135  }
136  }
137 
138  public static function cropCases(): array
139  {
140  return [
141  [0, 100, 1000, false],
142  [0, 1000, 100, true],
143  [50, 100, 75, true],
144  [50, 100, 200, false],
145  [100, 100, 50, true]
146  ];
147  }
148 }
testNegativeStart(Range $range)
testFactory
Definition: RangeTest.php:92
testConstructionWrongStart()
Definition: RangeTest.php:107
testNegativeLength(Range $range)
testFactory
Definition: RangeTest.php:101
static cropCases()
Definition: RangeTest.php:138
testFactory()
Definition: RangeTest.php:29
testEndCalculation(Range $range)
testFactory
Definition: RangeTest.php:49
testConstructionWrongLength()
Definition: RangeTest.php:114
testWithStart(Range $range)
testWithLength
Definition: RangeTest.php:69
croppedTo(int $max)
This will create a range that is guaranteed to not exceed $max.
Definition: Range.php:98
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
withLength(int $length)
Definition: Range.php:87
testWithLength(Range $range)
testFactory
Definition: RangeTest.php:57
testCroppedTo($start, $length, $max, $has_changed)
cropCases
Definition: RangeTest.php:124
withStart(int $start)
Definition: Range.php:79
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:28
testValues(Range $range)
testFactory
Definition: RangeTest.php:40
testUnpack(Range $range)
testWithStart
Definition: RangeTest.php:81