Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
test_character.py642 linesDownload Raw Back to tests
1import textwrap
2
3import pytest
4
5import numpy as np
6from numpy.f2py.tests import util
7from numpy.testing import assert_array_equal, assert_equal, assert_raises
8
9
10@pytest.mark.slow
11class TestCharacterString(util.F2PyTest):
12    # options = ['--debug-capi', '--build-dir', '/tmp/test-build-f2py']
13    suffix = '.f90'
14    fprefix = 'test_character_string'
15    length_list = ['1', '3', 'star']
16
17    code = ''
18    for length in length_list:
19        fsuffix = length
20        clength = {'star': '(*)'}.get(length, length)
21
22        code += textwrap.dedent(f"""
23
24        subroutine {fprefix}_input_{fsuffix}(c, o, n)
25          character*{clength}, intent(in) :: c
26          integer n
27          !f2py integer, depend(c), intent(hide) :: n = slen(c)
28          integer*1, dimension(n) :: o
29          !f2py intent(out) o
30          o = transfer(c, o)
31        end subroutine {fprefix}_input_{fsuffix}
32
33        subroutine {fprefix}_output_{fsuffix}(c, o, n)
34          character*{clength}, intent(out) :: c
35          integer n
36          integer*1, dimension(n), intent(in) :: o
37          !f2py integer, depend(o), intent(hide) :: n = len(o)
38          c = transfer(o, c)
39        end subroutine {fprefix}_output_{fsuffix}
40
41        subroutine {fprefix}_array_input_{fsuffix}(c, o, m, n)
42          integer m, i, n
43          character*{clength}, intent(in), dimension(m) :: c
44          !f2py integer, depend(c), intent(hide) :: m = len(c)
45          !f2py integer, depend(c), intent(hide) :: n = f2py_itemsize(c)
46          integer*1, dimension(m, n), intent(out) :: o
47          do i=1,m
48            o(i, :) = transfer(c(i), o(i, :))
49          end do
50        end subroutine {fprefix}_array_input_{fsuffix}
51
52        subroutine {fprefix}_array_output_{fsuffix}(c, o, m, n)
53          character*{clength}, intent(out), dimension(m) :: c
54          integer n
55          integer*1, dimension(m, n), intent(in) :: o
56          !f2py character(f2py_len=n) :: c
57          !f2py integer, depend(o), intent(hide) :: m = len(o)
58          !f2py integer, depend(o), intent(hide) :: n = shape(o, 1)
59          do i=1,m
60            c(i) = transfer(o(i, :), c(i))
61          end do
62        end subroutine {fprefix}_array_output_{fsuffix}
63
64        subroutine {fprefix}_2d_array_input_{fsuffix}(c, o, m1, m2, n)
65          integer m1, m2, i, j, n
66          character*{clength}, intent(in), dimension(m1, m2) :: c
67          !f2py integer, depend(c), intent(hide) :: m1 = len(c)
68          !f2py integer, depend(c), intent(hide) :: m2 = shape(c, 1)
69          !f2py integer, depend(c), intent(hide) :: n = f2py_itemsize(c)
70          integer*1, dimension(m1, m2, n), intent(out) :: o
71          do i=1,m1
72            do j=1,m2
73              o(i, j, :) = transfer(c(i, j), o(i, j, :))
74            end do
75          end do
76        end subroutine {fprefix}_2d_array_input_{fsuffix}
77        """)
78
79    @pytest.mark.parametrize("length", length_list)
80    def test_input(self, length):
81        fsuffix = {'(*)': 'star'}.get(length, length)
82        f = getattr(self.module, self.fprefix + '_input_' + fsuffix)
83
84        a = {'1': 'a', '3': 'abc', 'star': 'abcde' * 3}[length]
85
86        assert_array_equal(f(a), np.array(list(map(ord, a)), dtype='u1'))
87
88    @pytest.mark.parametrize("length", length_list[:-1])
89    def test_output(self, length):
90        fsuffix = length
91        f = getattr(self.module, self.fprefix + '_output_' + fsuffix)
92
93        a = {'1': 'a', '3': 'abc'}[length]
94
95        assert_array_equal(f(np.array(list(map(ord, a)), dtype='u1')),
96                           a.encode())
97
98    @pytest.mark.parametrize("length", length_list)
99    def test_array_input(self, length):
100        fsuffix = length
101        f = getattr(self.module, self.fprefix + '_array_input_' + fsuffix)
102
103        a = np.array([{'1': 'a', '3': 'abc', 'star': 'abcde' * 3}[length],
104                      {'1': 'A', '3': 'ABC', 'star': 'ABCDE' * 3}[length],
105                      ], dtype='S')
106
107        expected = np.array([list(s) for s in a], dtype='u1')
108        assert_array_equal(f(a), expected)
109
110    @pytest.mark.parametrize("length", length_list)
111    def test_array_output(self, length):
112        fsuffix = length
113        f = getattr(self.module, self.fprefix + '_array_output_' + fsuffix)
114
115        expected = np.array(
116            [{'1': 'a', '3': 'abc', 'star': 'abcde' * 3}[length],
117             {'1': 'A', '3': 'ABC', 'star': 'ABCDE' * 3}[length]], dtype='S')
118
119        a = np.array([list(s) for s in expected], dtype='u1')
120        assert_array_equal(f(a), expected)
121
122    @pytest.mark.parametrize("length", length_list)
123    def test_2d_array_input(self, length):
124        fsuffix = length
125        f = getattr(self.module, self.fprefix + '_2d_array_input_' + fsuffix)
126
127        a = np.array([[{'1': 'a', '3': 'abc', 'star': 'abcde' * 3}[length],
128                       {'1': 'A', '3': 'ABC', 'star': 'ABCDE' * 3}[length]],
129                      [{'1': 'f', '3': 'fgh', 'star': 'fghij' * 3}[length],
130                       {'1': 'F', '3': 'FGH', 'star': 'FGHIJ' * 3}[length]]],
131                     dtype='S')
132        expected = np.array([[list(item) for item in row] for row in a],
133                            dtype='u1', order='F')
134        assert_array_equal(f(a), expected)
135
136
137class TestCharacter(util.F2PyTest):
138    # options = ['--debug-capi', '--build-dir', '/tmp/test-build-f2py']
139    suffix = '.f90'
140    fprefix = 'test_character'
141
142    code = textwrap.dedent(f"""
143       subroutine {fprefix}_input(c, o)
144          character, intent(in) :: c
145          integer*1 o
146          !f2py intent(out) o
147          o = transfer(c, o)
148       end subroutine {fprefix}_input
149
150       subroutine {fprefix}_output(c, o)
151          character :: c
152          integer*1, intent(in) :: o
153          !f2py intent(out) c
154          c = transfer(o, c)
155       end subroutine {fprefix}_output
156
157       subroutine {fprefix}_input_output(c, o)
158          character, intent(in) :: c
159          character o
160          !f2py intent(out) o
161          o = c
162       end subroutine {fprefix}_input_output
163
164       subroutine {fprefix}_inout(c, n)
165          character :: c, n
166          !f2py intent(in) n
167          !f2py intent(inout) c
168          c = n
169       end subroutine {fprefix}_inout
170
171       function {fprefix}_return(o) result (c)
172          character :: c
173          character, intent(in) :: o
174          c = transfer(o, c)
175       end function {fprefix}_return
176
177       subroutine {fprefix}_array_input(c, o)
178          character, intent(in) :: c(3)
179          integer*1 o(3)
180          !f2py intent(out) o
181          integer i
182          do i=1,3
183            o(i) = transfer(c(i), o(i))
184          end do
185       end subroutine {fprefix}_array_input
186
187       subroutine {fprefix}_2d_array_input(c, o)
188          character, intent(in) :: c(2, 3)
189          integer*1 o(2, 3)
190          !f2py intent(out) o
191          integer i, j
192          do i=1,2
193            do j=1,3
194              o(i, j) = transfer(c(i, j), o(i, j))
195            end do
196          end do
197       end subroutine {fprefix}_2d_array_input
198
199       subroutine {fprefix}_array_output(c, o)
200          character :: c(3)
201          integer*1, intent(in) :: o(3)
202          !f2py intent(out) c
203          do i=1,3
204            c(i) = transfer(o(i), c(i))
205          end do
206       end subroutine {fprefix}_array_output
207
208       subroutine {fprefix}_array_inout(c, n)
209          character :: c(3), n(3)
210          !f2py intent(in) n(3)
211          !f2py intent(inout) c(3)
212          do i=1,3
213            c(i) = n(i)
214          end do
215       end subroutine {fprefix}_array_inout
216
217       subroutine {fprefix}_2d_array_inout(c, n)
218          character :: c(2, 3), n(2, 3)
219          !f2py intent(in) n(2, 3)
220          !f2py intent(inout) c(2. 3)
221          integer i, j
222          do i=1,2
223            do j=1,3
224              c(i, j) = n(i, j)
225            end do
226          end do
227       end subroutine {fprefix}_2d_array_inout
228
229       function {fprefix}_array_return(o) result (c)
230          character, dimension(3) :: c
231          character, intent(in) :: o(3)
232          do i=1,3
233            c(i) = o(i)
234          end do
235       end function {fprefix}_array_return
236
237       function {fprefix}_optional(o) result (c)
238          character, intent(in) :: o
239          !f2py character o = "a"
240          character :: c
241          c = o
242       end function {fprefix}_optional
243    """)
244
245    @pytest.mark.parametrize("dtype", ['c', 'S1'])
246    def test_input(self, dtype):
247        f = getattr(self.module, self.fprefix + '_input')
248
249        assert_equal(f(np.array('a', dtype=dtype)), ord('a'))
250        assert_equal(f(np.array(b'a', dtype=dtype)), ord('a'))
251        assert_equal(f(np.array(['a'], dtype=dtype)), ord('a'))
252        assert_equal(f(np.array('abc', dtype=dtype)), ord('a'))
253        assert_equal(f(np.array([['a']], dtype=dtype)), ord('a'))
254
255    def test_input_varia(self):
256        f = getattr(self.module, self.fprefix + '_input')
257
258        assert_equal(f('a'), ord('a'))
259        assert_equal(f(b'a'), ord(b'a'))
260        assert_equal(f(''), 0)
261        assert_equal(f(b''), 0)
262        assert_equal(f(b'\0'), 0)
263        assert_equal(f('ab'), ord('a'))
264        assert_equal(f(b'ab'), ord('a'))
265        assert_equal(f(['a']), ord('a'))
266
267        assert_equal(f(np.array(b'a')), ord('a'))
268        assert_equal(f(np.array([b'a'])), ord('a'))
269        a = np.array('a')
270        assert_equal(f(a), ord('a'))
271        a = np.array(['a'])
272        assert_equal(f(a), ord('a'))
273
274        try:
275            f([])
276        except IndexError as msg:
277            if not str(msg).endswith(' got 0-list'):
278                raise
279        else:
280            raise SystemError(f'{f.__name__} should have failed on empty list')
281
282        try:
283            f(97)
284        except TypeError as msg:
285            if not str(msg).endswith(' got int instance'):
286                raise
287        else:
288            raise SystemError(f'{f.__name__} should have failed on int value')
289
290    @pytest.mark.parametrize("dtype", ['c', 'S1', 'U1'])
291    def test_array_input(self, dtype):
292        f = getattr(self.module, self.fprefix + '_array_input')
293
294        assert_array_equal(f(np.array(['a', 'b', 'c'], dtype=dtype)),
295                           np.array(list(map(ord, 'abc')), dtype='i1'))
296        assert_array_equal(f(np.array([b'a', b'b', b'c'], dtype=dtype)),
297                           np.array(list(map(ord, 'abc')), dtype='i1'))
298
299    def test_array_input_varia(self):
300        f = getattr(self.module, self.fprefix + '_array_input')
301        assert_array_equal(f(['a', 'b', 'c']),
302                           np.array(list(map(ord, 'abc')), dtype='i1'))
303        assert_array_equal(f([b'a', b'b', b'c']),
304                           np.array(list(map(ord, 'abc')), dtype='i1'))
305
306        try:
307            f(['a', 'b', 'c', 'd'])
308        except ValueError as msg:
309            if not str(msg).endswith(
310                    'th dimension must be fixed to 3 but got 4'):
311                raise
312        else:
313            raise SystemError(
314                f'{f.__name__} should have failed on wrong input')
315
316    @pytest.mark.parametrize("dtype", ['c', 'S1', 'U1'])
317    def test_2d_array_input(self, dtype):
318        f = getattr(self.module, self.fprefix + '_2d_array_input')
319
320        a = np.array([['a', 'b', 'c'],
321                      ['d', 'e', 'f']], dtype=dtype, order='F')
322        expected = a.view(np.uint32 if dtype == 'U1' else np.uint8)
323        assert_array_equal(f(a), expected)
324
325    def test_output(self):
326        f = getattr(self.module, self.fprefix + '_output')
327
328        assert_equal(f(ord(b'a')), b'a')
329        assert_equal(f(0), b'\0')
330
331    def test_array_output(self):
332        f = getattr(self.module, self.fprefix + '_array_output')
333
334        assert_array_equal(f(list(map(ord, 'abc'))),
335                           np.array(list('abc'), dtype='S1'))
336
337    def test_input_output(self):
338        f = getattr(self.module, self.fprefix + '_input_output')
339
340        assert_equal(f(b'a'), b'a')
341        assert_equal(f('a'), b'a')
342        assert_equal(f(''), b'\0')
343
344    @pytest.mark.parametrize("dtype", ['c', 'S1'])
345    def test_inout(self, dtype):
346        f = getattr(self.module, self.fprefix + '_inout')
347
348        a = np.array(list('abc'), dtype=dtype)
349        f(a, 'A')
350        assert_array_equal(a, np.array(list('Abc'), dtype=a.dtype))
351        f(a[1:], 'B')
352        assert_array_equal(a, np.array(list('ABc'), dtype=a.dtype))
353
354        a = np.array(['abc'], dtype=dtype)
355        f(a, 'A')
356        assert_array_equal(a, np.array(['Abc'], dtype=a.dtype))
357
358    def test_inout_varia(self):
359        f = getattr(self.module, self.fprefix + '_inout')
360        a = np.array('abc', dtype='S3')
361        f(a, 'A')
362        assert_array_equal(a, np.array('Abc', dtype=a.dtype))
363
364        a = np.array(['abc'], dtype='S3')
365        f(a, 'A')
366        assert_array_equal(a, np.array(['Abc'], dtype=a.dtype))
367
368        try:
369            f('abc', 'A')
370        except ValueError as msg:
371            if not str(msg).endswith(' got 3-str'):
372                raise
373        else:
374            raise SystemError(f'{f.__name__} should have failed on str value')
375
376    @pytest.mark.parametrize("dtype", ['c', 'S1'])
377    def test_array_inout(self, dtype):
378        f = getattr(self.module, self.fprefix + '_array_inout')
379        n = np.array(['A', 'B', 'C'], dtype=dtype, order='F')
380
381        a = np.array(['a', 'b', 'c'], dtype=dtype, order='F')
382        f(a, n)
383        assert_array_equal(a, n)
384
385        a = np.array(['a', 'b', 'c', 'd'], dtype=dtype)
386        f(a[1:], n)
387        assert_array_equal(a, np.array(['a', 'A', 'B', 'C'], dtype=dtype))
388
389        a = np.array([['a', 'b', 'c']], dtype=dtype, order='F')
390        f(a, n)
391        assert_array_equal(a, np.array([['A', 'B', 'C']], dtype=dtype))
392
393        a = np.array(['a', 'b', 'c', 'd'], dtype=dtype, order='F')
394        try:
395            f(a, n)
396        except ValueError as msg:
397            if not str(msg).endswith(
398                    'th dimension must be fixed to 3 but got 4'):
399                raise
400        else:
401            raise SystemError(
402                f'{f.__name__} should have failed on wrong input')
403
404    @pytest.mark.parametrize("dtype", ['c', 'S1'])
405    def test_2d_array_inout(self, dtype):
406        f = getattr(self.module, self.fprefix + '_2d_array_inout')
407        n = np.array([['A', 'B', 'C'],
408                      ['D', 'E', 'F']],
409                     dtype=dtype, order='F')
410        a = np.array([['a', 'b', 'c'],
411                      ['d', 'e', 'f']],
412                     dtype=dtype, order='F')
413        f(a, n)
414        assert_array_equal(a, n)
415
416    def test_return(self):
417        f = getattr(self.module, self.fprefix + '_return')
418
419        assert_equal(f('a'), b'a')
420
421    @pytest.mark.skip('fortran function returning array segfaults')
422    def test_array_return(self):
423        f = getattr(self.module, self.fprefix + '_array_return')
424
425        a = np.array(list('abc'), dtype='S1')
426        assert_array_equal(f(a), a)
427
428    def test_optional(self):
429        f = getattr(self.module, self.fprefix + '_optional')
430
431        assert_equal(f(), b"a")
432        assert_equal(f(b'B'), b"B")
433
434
435class TestMiscCharacter(util.F2PyTest):
436    # options = ['--debug-capi', '--build-dir', '/tmp/test-build-f2py']
437    suffix = '.f90'
438    fprefix = 'test_misc_character'
439
440    code = textwrap.dedent(f"""
441       subroutine {fprefix}_gh18684(x, y, m)
442         character(len=5), dimension(m), intent(in) :: x
443         character*5, dimension(m), intent(out) :: y
444         integer i, m
445         !f2py integer, intent(hide), depend(x) :: m = f2py_len(x)
446         do i=1,m
447           y(i) = x(i)
448         end do
449       end subroutine {fprefix}_gh18684
450
451       subroutine {fprefix}_gh6308(x, i)
452         integer i
453         !f2py check(i>=0 && i<12) i
454         character*5 name, x
455         common name(12)
456         name(i + 1) = x
457       end subroutine {fprefix}_gh6308
458
459       subroutine {fprefix}_gh4519(x)
460         character(len=*), intent(in) :: x(:)
461         !f2py intent(out) x
462         integer :: i
463         ! Uncomment for debug printing:
464         !do i=1, size(x)
465         !   print*, "x(",i,")=", x(i)
466         !end do
467       end subroutine {fprefix}_gh4519
468
469       pure function {fprefix}_gh3425(x) result (y)
470         character(len=*), intent(in) :: x
471         character(len=len(x)) :: y
472         integer :: i
473         do i = 1, len(x)
474           j = iachar(x(i:i))
475           if (j>=iachar("a") .and. j<=iachar("z") ) then
476             y(i:i) = achar(j-32)
477           else
478             y(i:i) = x(i:i)
479           endif
480         end do
481       end function {fprefix}_gh3425
482
483       subroutine {fprefix}_character_bc_new(x, y, z)
484         character, intent(in) :: x
485         character, intent(out) :: y
486         !f2py character, depend(x) :: y = x
487         !f2py character, dimension((x=='a'?1:2)), depend(x), intent(out) :: z
488         character, dimension(*) :: z
489         !f2py character, optional, check(x == 'a' || x == 'b') :: x = 'a'
490         !f2py callstatement (*f2py_func)(&x, &y, z)
491         !f2py callprotoargument character*, character*, character*
492         if (y.eq.x) then
493           y = x
494         else
495           y = 'e'
496         endif
497         z(1) = 'c'
498       end subroutine {fprefix}_character_bc_new
499
500       subroutine {fprefix}_character_bc_old(x, y, z)
501         character, intent(in) :: x
502         character, intent(out) :: y
503         !f2py character, depend(x) :: y = x[0]
504         !f2py character, dimension((*x=='a'?1:2)), depend(x), intent(out) :: z
505         character, dimension(*) :: z
506         !f2py character, optional, check(*x == 'a' || x[0] == 'b') :: x = 'a'
507         !f2py callstatement (*f2py_func)(x, y, z)
508         !f2py callprotoargument char*, char*, char*
509          if (y.eq.x) then
510           y = x
511         else
512           y = 'e'
513         endif
514         z(1) = 'c'
515       end subroutine {fprefix}_character_bc_old
516    """)
517
518    @pytest.mark.slow
519    def test_gh18684(self):
520        # Test character(len=5) and character*5 usages
521        f = getattr(self.module, self.fprefix + '_gh18684')
522        x = np.array(["abcde", "fghij"], dtype='S5')
523        y = f(x)
524
525        assert_array_equal(x, y)
526
527    def test_gh6308(self):
528        # Test character string array in a common block
529        f = getattr(self.module, self.fprefix + '_gh6308')
530
531        assert_equal(self.module._BLNK_.name.dtype, np.dtype('S5'))
532        assert_equal(len(self.module._BLNK_.name), 12)
533        f("abcde", 0)
534        assert_equal(self.module._BLNK_.name[0], b"abcde")
535        f("12345", 5)
536        assert_equal(self.module._BLNK_.name[5], b"12345")
537
538    def test_gh4519(self):
539        # Test array of assumed length strings
540        f = getattr(self.module, self.fprefix + '_gh4519')
541
542        for x, expected in [
543                ('a', {'shape': (), 'dtype': np.dtype('S1')}),
544                ('text', {'shape': (), 'dtype': np.dtype('S4')}),
545                (np.array(['1', '2', '3'], dtype='S1'),
546                 {'shape': (3,), 'dtype': np.dtype('S1')}),
547                (['1', '2', '34'],
548                 {'shape': (3,), 'dtype': np.dtype('S2')}),
549                (['', ''], {'shape': (2,), 'dtype': np.dtype('S1')})]:
550            r = f(x)
551            for k, v in expected.items():
552                assert_equal(getattr(r, k), v)
553
554    def test_gh3425(self):
555        # Test returning a copy of assumed length string
556        f = getattr(self.module, self.fprefix + '_gh3425')
557        # f is equivalent to bytes.upper
558
559        assert_equal(f('abC'), b'ABC')
560        assert_equal(f(''), b'')
561        assert_equal(f('abC12d'), b'ABC12D')
562
563    @pytest.mark.parametrize("state", ['new', 'old'])
564    def test_character_bc(self, state):
565        f = getattr(self.module, self.fprefix + '_character_bc_' + state)
566
567        c, a = f()
568        assert_equal(c, b'a')
569        assert_equal(len(a), 1)
570
571        c, a = f(b'b')
572        assert_equal(c, b'b')
573        assert_equal(len(a), 2)
574
575        assert_raises(Exception, lambda: f(b'c'))
576
577
578class TestStringScalarArr(util.F2PyTest):
579    sources = [util.getpath("tests", "src", "string", "scalar_string.f90")]
580
581    def test_char(self):
582        for out in (self.module.string_test.string,
583                    self.module.string_test.string77):
584            expected = ()
585            assert out.shape == expected
586            expected = '|S8'
587            assert out.dtype == expected
588
589    def test_char_arr(self):
590        for out in (self.module.string_test.strarr,
591                    self.module.string_test.strarr77):
592            expected = (5, 7)
593            assert out.shape == expected
594            expected = '|S12'
595            assert out.dtype == expected
596
597class TestStringAssumedLength(util.F2PyTest):
598    sources = [util.getpath("tests", "src", "string", "gh24008.f")]
599
600    def test_gh24008(self):
601        self.module.greet("joe", "bob")
602
603@pytest.mark.slow
604class TestStringOptionalInOut(util.F2PyTest):
605    sources = [util.getpath("tests", "src", "string", "gh24662.f90")]
606
607    def test_gh24662(self):
608        self.module.string_inout_optional()
609        a = np.array('hi', dtype='S32')
610        self.module.string_inout_optional(a)
611        assert "output string" in a.tobytes().decode()
612        with pytest.raises(Exception):  # noqa: B017
613            aa = "Hi"
614            self.module.string_inout_optional(aa)
615
616
617@pytest.mark.slow
618class TestNewCharHandling(util.F2PyTest):
619    # from v1.24 onwards, gh-19388
620    sources = [
621        util.getpath("tests", "src", "string", "gh25286.pyf"),
622        util.getpath("tests", "src", "string", "gh25286.f90")
623    ]
624    module_name = "_char_handling_test"
625
626    def test_gh25286(self):
627        info = self.module.charint('T')
628        assert info == 2
629
630@pytest.mark.slow
631class TestBCCharHandling(util.F2PyTest):
632    # SciPy style, "incorrect" bindings with a hook
633    sources = [
634        util.getpath("tests", "src", "string", "gh25286_bc.pyf"),
635        util.getpath("tests", "src", "string", "gh25286.f90")
636    ]
637    module_name = "_char_handling_test"
638
639    def test_gh25286(self):
640        info = self.module.charint('T')
641        assert info == 2
642 
codekingpro/portable-devtools · Team Ai