This repository has been archived by the owner on Feb 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from hpat.tests_perf.test_perf_unicode import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# -*- coding: utf-8 -*- | ||
import unittest | ||
|
||
from numba import njit, types | ||
|
||
import hpat | ||
|
||
STRING_CASES = [ | ||
'ascii', | ||
'1234567890', | ||
'¡Y tú quién te crees?', | ||
'🐍⚡', | ||
'大处着眼,小处着手。', | ||
] | ||
|
||
|
||
def center_usecase(x, y): | ||
return x.center(y) | ||
|
||
|
||
def center_usecase_fillchar(x, y, fillchar): | ||
return x.center(y, fillchar) | ||
|
||
|
||
def ljust_usecase(x, y): | ||
return x.ljust(y) | ||
|
||
|
||
def ljust_usecase_fillchar(x, y, fillchar): | ||
return x.ljust(y, fillchar) | ||
|
||
|
||
def rjust_usecase(x, y): | ||
return x.rjust(y) | ||
|
||
|
||
def rjust_usecase_fillchar(x, y, fillchar): | ||
return x.rjust(y, fillchar) | ||
|
||
|
||
class TestUnicodeStrings(unittest.TestCase): | ||
""" | ||
Test unicode strings operations | ||
""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
|
||
self._test_iteration = 10 | ||
|
||
def test_justification(self): | ||
for pyfunc, case_name in [(center_usecase, 'center'), | ||
(ljust_usecase, 'ljust'), | ||
(rjust_usecase, 'rjust')]: | ||
|
||
# compilation time is also needs to be measured | ||
cfunc = njit(pyfunc) | ||
|
||
for iter in range(self._test_iteration): | ||
for s in STRING_CASES: | ||
for width in range(-3, 20): | ||
# these function calls needs to be benchmarked | ||
pyfunc_result = pyfunc(s, width) | ||
cfunc_result = cfunc(s, width) | ||
|
||
self.assertEqual(pyfunc_result, cfunc_result, | ||
"'%s'.%s(%d)?" % (s, case_name, width)) | ||
|
||
def test_justification_fillchar(self): | ||
for pyfunc, case_name in [(center_usecase_fillchar, 'center'), | ||
(ljust_usecase_fillchar, 'ljust'), | ||
(rjust_usecase_fillchar, 'rjust')]: | ||
|
||
# compilation time is also needs to be measured | ||
cfunc = njit(pyfunc) | ||
|
||
for iter in range(self._test_iteration): | ||
# allowed fillchar cases | ||
for fillchar in [' ', '+', 'ú', '处']: | ||
for s in STRING_CASES: | ||
for width in range(-3, 20): | ||
# these function calls needs to be benchmarked | ||
pyfunc_result = pyfunc(s, width, fillchar) | ||
cfunc_result = cfunc(s, width, fillchar) | ||
|
||
self.assertEqual(pyfunc_result, cfunc_result, | ||
"'%s'.%s(%d, '%s')?" % (s, case_name, width, fillchar)) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |