@@ -92,6 +92,41 @@ def test_unit_scale(self, mock_terminal_size, mock_stderr):
92
92
self ._compare_bars (tinytqdm_output , tqdm_output )
93
93
if n > 3 : break
94
94
95
+ @patch ('sys.stderr' , new_callable = StringIO )
96
+ @patch ('shutil.get_terminal_size' )
97
+ def test_unit_scale_exact (self , mock_terminal_size , mock_stderr ):
98
+ unit_scale = True
99
+ ncols = 80
100
+ mock_terminal_size .return_value = namedtuple (field_names = 'columns' , typename = 'terminal_size' )(ncols )
101
+ mock_stderr .truncate (0 )
102
+
103
+ total = 10
104
+ with patch ('time.perf_counter' , side_effect = [0 ]+ list (range (100 ))): # one more 0 for the init call
105
+ # compare bars at each iteration (only when tinytqdm bar has been updated)
106
+ for n in tinytqdm (range (total ), desc = "Test" , total = total , unit_scale = unit_scale , rate = 1e9 ):
107
+ tinytqdm_output = mock_stderr .getvalue ().split ("\r " )[- 1 ].rstrip ()
108
+ elapsed = n
109
+ tqdm_output = tqdm .format_meter (n = n , total = total , elapsed = elapsed , ncols = ncols , prefix = "Test" , unit_scale = unit_scale )
110
+ self ._compare_bars (tinytqdm_output , tqdm_output )
111
+ if n > 5 : break
112
+
113
+ total = 10
114
+ k = 0.001000001
115
+ # regression test for
116
+ # E AssertionError: ' 1.00/10.0 1000it/s]' != ' 1.00/10.0 1.00kit/s]'
117
+ # E - 1.00/10.0 1000it/s]
118
+ # E ? ^
119
+ # E + 1.00/10.0 1.00kit/s]
120
+ # E ? + ^
121
+ with patch ('time.perf_counter' , side_effect = [0 , * [i * k for i in range (100 )]]): # one more 0 for the init call
122
+ # compare bars at each iteration (only when tinytqdm bar has been updated)
123
+ for n in tinytqdm (range (total ), desc = "Test" , total = total , unit_scale = unit_scale , rate = 1e9 ):
124
+ tinytqdm_output = mock_stderr .getvalue ().split ("\r " )[- 1 ].rstrip ()
125
+ elapsed = n * k
126
+ tqdm_output = tqdm .format_meter (n = n , total = total , elapsed = elapsed , ncols = ncols , prefix = "Test" , unit_scale = unit_scale )
127
+ self ._compare_bars (tinytqdm_output , tqdm_output )
128
+ if n > 5 : break
129
+
95
130
@patch ('sys.stderr' , new_callable = StringIO )
96
131
@patch ('shutil.get_terminal_size' )
97
132
def test_set_description (self , mock_terminal_size , mock_stderr ):
0 commit comments