<commit_before>"""
Test cases adapted from numba/tests/test_enums.py
"""

import numpy as np

from numba import int8, int16, int32
from numba import cuda, vectorize
from numba.cuda.testing import unittest, CUDATestCase
from numba.tests.enum_usecases import (
    Color,
    Shape,
    Planet,
    RequestError,
    IntEnumWithNegatives
)


class EnumTest(CUDATestCase):

    pairs = [
        (Color.red, Color.red),
        (Color.red, Color.green),
        (Planet.EARTH, Planet.EARTH),
        (Planet.VENUS, Planet.MARS),
        (Shape.circle, IntEnumWithNegatives.two) # IntEnum, same value
    ]

    def test_compare(self):
        @cuda.jit
        def f(a, b, out):
            out[0] = a == b
            out[1] = a != b
            out[2] = a is b
            out[3] = a is not b

        arr = np.zeros((4,), dtype=np.bool_)
        for a, b in self.pairs:
            f[1, 1](a, b, arr)
            self.assertPreciseEqual(arr, np.array([
                a == b, a != b, a is b, a is not b
            ]))

    def test_getattr_getitem(self):
        @cuda.jit
        def f(out):
            # Lookup of an enum member on its class
            out[0] = Color.red == Color.green
            out[1] = Color['red'] == Color['green']

        arr = np.zeros((2,), dtype=np.bool_)
        f[1, 1](arr)
        self.assertPreciseEqual(arr, np.array([
            Color.red == Color.green, Color['red'] == Color['green']
        ]))

    def test_return_from_device_func(self):
        @cuda.jit(device=True)
        def helper(pred):
            return Color.red if pred else Color.green

        @cuda.jit
        def f(pred, out):
            out[0] = helper(pred) == Color.red
            out[1] = helper(not pred) == Color.green

        arr = np.zeros((2,), dtype=np.bool_)
        f[1, 1](True, arr)
        self.assertPreciseEqual(arr, np.array([
            Color.red == Color.red, Color.green == Color.green
        ]))

    def test_int_coerce(self):
        @cuda.jit
        def f(out):
            # Implicit coercion of intenums to ints
            out[0] = Shape.square - RequestError.not_found
            out[1] = Shape.circle > IntEnumWithNegatives.one

        arr = np.zeros((2,), dtype=np.int32)
        f[1, 1](arr)
        self.assertPreciseEqual(arr, np.array([
            Shape.square - RequestError.not_found,
            Shape.circle > IntEnumWithNegatives.one
        ], dtype=arr.dtype))

    def test_int_cast(self):
        @cuda.jit
        def f(x, out0, out1, out2):
            # Explicit coercion of intenums to ints
            out0[0] = x > int16(RequestError.internal_error)
            out1[0] = x - int32(RequestError.not_found)
            out2[0] = x + int8(Shape.circle)

        arr0 = np.zeros((1,), dtype=np.int16)
        arr1 = np.zeros((1,), dtype=np.int32)
        arr2 = np.zeros((1,), dtype=np.int8)
        x = np.int8(10)

        f[1, 1](x, arr0, arr1, arr2)
        self.assertEqual(arr0[0], x > np.int16(RequestError.internal_error))
        self.assertEqual(arr1[0], x - np.int32(RequestError.not_found))
        self.assertEqual(arr2[0], x + np.int8(Shape.circle))

    def test_vectorize(self):
        def f(x):
            if x != RequestError.not_found:
                return RequestError['internal_error']
            else:
                return RequestError.dummy

        cuda_func = vectorize("int64(int64)", target='cuda')(f)
        arr = np.array([2, 404, 500, 404])
        expected = np.array([f(x) for x in arr])
        got = cuda_func(arr)<commit_msg>Skip ufunc tests on simulator<commit_after>"""
Test cases adapted from numba/tests/test_enums.py
"""

import numpy as np

from numba import int8, int16, int32
from numba import cuda, vectorize
from numba.cuda.testing import unittest, CUDATestCase, skip_on_cudasim
from numba.tests.enum_usecases import (
    Color,
    Shape,
    Planet,
    RequestError,
    IntEnumWithNegatives
)


class EnumTest(CUDATestCase):

    pairs = [
        (Color.red, Color.red),
        (Color.red, Color.green),
        (Planet.EARTH, Planet.EARTH),
        (Planet.VENUS, Planet.MARS),
        (Shape.circle, IntEnumWithNegatives.two) # IntEnum, same value
    ]

    def test_compare(self):
        @cuda.jit
        def f(a, b, out):
            out[0] = a == b
            out[1] = a != b
            out[2] = a is b
            out[3] = a is not b

        arr = np.zeros((4,), dtype=np.bool_)
        for a, b in self.pairs:
            f[1, 1](a, b, arr)
            self.assertPreciseEqual(arr, np.array([
                a == b, a != b, a is b, a is not b
            ]))

    def test_getattr_getitem(self):
        @cuda.jit
        def f(out):
            # Lookup of an enum member on its class
            out[0] = Color.red == Color.green
            out[1] = Color['red'] == Color['green']

        arr = np.zeros((2,), dtype=np.bool_)
        f[1, 1](arr)
        self.assertPreciseEqual(arr, np.array([
            Color.red == Color.green, Color['red'] == Color['green']
        ]))

    def test_return_from_device_func(self):
        @cuda.jit(device=True)
        def helper(pred):
            return Color.red if pred else Color.green

        @cuda.jit
        def f(pred, out):
            out[0] = helper(pred) == Color.red
            out[1] = helper(not pred) == Color.green

        arr = np.zeros((2,), dtype=np.bool_)
        f[1, 1](True, arr)
        self.assertPreciseEqual(arr, np.array([
            Color.red == Color.red, Color.green == Color.green
        ]))

    def test_int_coerce(self):
        @cuda.jit
        def f(out):
            # Implicit coercion of intenums to ints
            out[0] = Shape.square - RequestError.not_found
            out[1] = Shape.circle > IntEnumWithNegatives.one

        arr = np.zeros((2,), dtype=np.int32)
        f[1, 1](arr)
        self.assertPreciseEqual(arr, np.array([
            Shape.square - RequestError.not_found,
            Shape.circle > IntEnumWithNegatives.one
        ], dtype=arr.dtype))

    def test_int_cast(self):
        @cuda.jit
        def f(x, out0, out1, out2):
            # Explicit coercion of intenums to ints
            out0[0] = x > int16(RequestError.internal_error)
            out1[0] = x - int32(RequestError.not_found)
            out2[0] = x + int8(Shape.circle)

        arr0 = np.zeros((1,), dtype=np.int16)
        arr1 = np.zeros((1,), dtype=np.int32)
        arr2 = np.zeros((1,), dtype=np.int8)
        x = np.int8(10)

        f[1, 1](x, arr0, arr1, arr2)
        self.assertEqual(arr0[0], x > np.int16(RequestError.internal_error))
        self.assertEqual(arr1[0], x - np.int32(RequestError.not_found))
        self.assertEqual(arr2[0], x + np.int8(Shape.circle))

    @skip_on_cudasim("ufuncs are unsupported on simulator.")
    def test_vectorize(self):
        def f(x):
            if x != RequestError.not_found:
                return RequestError['internal_error']
            else:
                return RequestError.dummy

        cuda_func = vectorize("int64(int64)", target='cuda')(f)
        arr = np.array([2, 404, 500, 404])
        expected = np.array([f(x) for x in arr])
        got = cuda_func(arr)
{'fraction_non_alphanumeric': 0.13315672058003347, 'fraction_numerical': 0.02426101505856107, 'mean_word_length': 1.9738805970149254, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5985307', 'n_tokens_mistral': 2548, 'n_tokens_neox': 2421, 'n_words': 541}