<commit_before># -*- coding: utf-8 -*-
import os
import math

import pytest

from msl import loadlib
from msl.loadlib import IS_MAC, IS_WINDOWS
from msl.examples.loadlib import Cpp64, Fortran64, Echo64, DotNet64, FourPoints

from conftest import (
    skipif_no_server32,
    skipif_not_windows,
)

c = None
f = None
e = None
n = None


def setup_module():
    global c, f, e, n
    if IS_MAC:  # the 32-bit server for macOS does not exist
        return
    c = Cpp64()
    f = Fortran64()
    e = Echo64()
    n = DotNet64()


def teardown_module():
    if IS_MAC:  # the 32-bit server for macOS does not exist
        return
    c.shutdown_server32()
    f.shutdown_server32()
    e.shutdown_server32()
    n.shutdown_server32()


@skipif_no_server32
def test_unique_ports():
    for item in [f, e, n]:
        assert c.port != item.port
    for item in [e, n]:
        assert f.port != item.port
    assert e.port != n.port


@skipif_no_server32
def test_lib_name():
    def get_name(path):
        return os.path.basename(path).split('.')[0]

    assert 'cpp_lib32' == get_name(c.lib32_path)
    assert 'fortran_lib32' == get_name(f.lib32_path)
    assert 'dotnet_lib32' == get_name(n.lib32_path)


@skipif_no_server32
def test_server_version():
    assert loadlib.Server32.version().startswith('Python')


@skipif_no_server32
def test_cpp():
    assert 3 == c.add(1, 2)
    assert -1002 == c.add(-1000, -2)
    assert 10.0 == pytest.approx(c.subtract(20.0, 10.0))
    assert -10.0 == pytest.approx(c.subtract(90.0, 100.0))
    assert 0.0 == pytest.approx(c.add_or_subtract(0.1234, -0.1234, True))
    assert 100.0 == pytest.approx(c.add_or_subtract(123.456, 23.456, False))

    a = 3.1415926
    values = [float(x) for x in range(100)]
    c_values = c.scalar_multiply(a, values)
    for i in range(len(values)):
        assert a*values[i] == pytest.approx(c_values[i])

    assert '0987654321' == c.reverse_string_v1('1234567890')
    assert '[abc x|z j 1 *&' == c.reverse_string_v2('&* 1 j z|x cba[')

    if loadlib.IS_PYTHON3:<commit_msg>only test DotNet64 on Windows<commit_after># -*- coding: utf-8 -*-
import os
import math

import pytest

from msl import loadlib
from msl.loadlib import IS_MAC, IS_WINDOWS
from msl.examples.loadlib import Cpp64, Fortran64, Echo64, DotNet64, FourPoints

from conftest import (
    skipif_no_server32,
    skipif_not_windows,
)

c = None
f = None
e = None
n = None


def setup_module():
    global c, f, e, n
    if IS_MAC:  # the 32-bit server for macOS does not exist
        return
    c = Cpp64()
    f = Fortran64()
    e = Echo64()
    if IS_WINDOWS:
        # stop testing on linux because of pythonnet issue #1210
        # mono 5.20 is getting too old, but it is the latest version
        # that works with pythonnet 2.4.0 and 32-bit linux
        n = DotNet64()


def teardown_module():
    if IS_MAC:  # the 32-bit server for macOS does not exist
        return
    c.shutdown_server32()
    f.shutdown_server32()
    e.shutdown_server32()
    if n is not None:
        n.shutdown_server32()


@skipif_no_server32
def test_unique_ports():
    for obj1 in [c, f, e, n]:
        for obj2 in [c, f, e, n]:
            if obj1 is None or obj2 is None or obj1 is obj2:
                continue
            assert obj1.port != obj2.port


@skipif_no_server32
def test_lib_name():
    def get_name(path):
        return os.path.basename(path).split('.')[0]

    assert 'cpp_lib32' == get_name(c.lib32_path)
    assert 'fortran_lib32' == get_name(f.lib32_path)
    if n is not None:
        assert 'dotnet_lib32' == get_name(n.lib32_path)


@skipif_no_server32
def test_server_version():
    assert loadlib.Server32.version().startswith('Python')


@skipif_no_server32
def test_cpp():
    assert 3 == c.add(1, 2)
    assert -1002 == c.add(-1000, -2)
    assert 10.0 == pytest.approx(c.subtract(20.0, 10.0))
    assert -10.0 == pytest.approx(c.subtract(90.0, 100.0))
    assert 0.0 == pytest.approx(c.add_or_subtract(0.1234, -0.1234, True))
    assert 100.0 == pytest.approx(c.add_or_subtract(123.456, 23.456, False))

    a = 3.1415926
    values = [float(x) for x in range(100)]
    c_values = c.scalar_multiply(a, values)
    for i in range(len(values)):
        assert a*values[i] == pytest.approx(c_values[i])

    assert '0987654321' == c.reverse_string_v1('1234567890')
    assert '[abc x|z j 1 *&' == c.reverse_string_v2('&* 1 j z|x cba[')

    if loadlib.IS_PYTHON3:
{'fraction_non_alphanumeric': 0.12519774011299434, 'fraction_numerical': 0.07186440677966102, 'mean_word_length': 2.998193315266486, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1321982', 'n_tokens_mistral': 1879, 'n_tokens_neox': 1673, 'n_words': 430}