| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use Litipk\BigNumbers\Decimal as Decimal; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | date_default_timezone_set('UTC'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  | class DecimalCompTest extends TestCase | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     public function testSelfComp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |         $ten  = Decimal::fromInteger(10); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         $this->assertTrue($ten->comp($ten) === 0); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 16 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     public function testBasicCases() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $one = Decimal::fromInteger(1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $ten = Decimal::fromInteger(10); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $this->assertTrue($one->comp($ten) === -1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $this->assertTrue($ten->comp($one) === 1); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public function testUnscaledComp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         // Transitivity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $this->assertEquals(-1, Decimal::fromFloat(1.001)->comp(Decimal::fromFloat(1.01))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $this->assertEquals(1, Decimal::fromFloat(1.01)->comp(Decimal::fromFloat(1.004))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->assertEquals(-1, Decimal::fromFloat(1.001)->comp(Decimal::fromFloat(1.004))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         // Reflexivity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->assertEquals(0, Decimal::fromFloat(1.00525)->comp(Decimal::fromFloat(1.00525))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         // Symmetry | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->assertEquals(1, Decimal::fromFloat(1.01)->comp(Decimal::fromFloat(1.001))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->assertEquals(-1, Decimal::fromFloat(1.004)->comp(Decimal::fromFloat(1.01))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->assertEquals(1, Decimal::fromFloat(1.004)->comp(Decimal::fromFloat(1.001))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->assertEquals(1, Decimal::fromFloat(1.004)->comp(Decimal::fromFloat(1.000))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         // Warning, float to Decimal conversion can have unexpected behaviors, like converting | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         // 1.005 to Decimal("1.0049999999999999") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->assertEquals(-1, Decimal::fromFloat(1.0050000000001)->comp(Decimal::fromFloat(1.010))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $this->assertEquals(-1, Decimal::fromString("1.005")->comp(Decimal::fromString("1.010"))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         # Proper rounding | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->assertEquals(-1, Decimal::fromFloat(1.004)->comp(Decimal::fromFloat(1.0050000000001))); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public function testScaledComp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         // Transitivity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->assertEquals(0, Decimal::fromFloat(1.001)->comp(Decimal::fromFloat(1.01), 1)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->assertEquals(0, Decimal::fromFloat(1.01)->comp(Decimal::fromFloat(1.004), 1)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $this->assertEquals(0, Decimal::fromFloat(1.001)->comp(Decimal::fromFloat(1.004), 1)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         // Reflexivity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $this->assertEquals(0, Decimal::fromFloat(1.00525)->comp(Decimal::fromFloat(1.00525), 2)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         // Symmetry | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $this->assertEquals(0, Decimal::fromFloat(1.01)->comp(Decimal::fromFloat(1.001), 1)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $this->assertEquals(0, Decimal::fromFloat(1.004)->comp(Decimal::fromFloat(1.01), 1)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $this->assertEquals(0, Decimal::fromFloat(1.004)->comp(Decimal::fromFloat(1.001), 1)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         // Proper rounding | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $this->assertEquals(0, Decimal::fromFloat(1.004)->comp(Decimal::fromFloat(1.000), 2)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         // Warning, float to Decimal conversion can have unexpected behaviors, like converting | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         // 1.005 to Decimal("1.0049999999999999") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $this->assertEquals(0, Decimal::fromFloat(1.0050000000001)->comp(Decimal::fromFloat(1.010), 2)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $this->assertEquals(0, Decimal::fromString("1.005")->comp(Decimal::fromString("1.010"), 2)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         # Proper rounding | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $this->assertEquals(-1, Decimal::fromFloat(1.004)->comp(Decimal::fromFloat(1.0050000000001), 2)); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 81 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |  |