Skip to content

Commit

Permalink
TST: add tests for config.py
Browse files Browse the repository at this point in the history
correct a comment in the config code
  • Loading branch information
grlee77 committed Sep 17, 2018
1 parent 554b20a commit 34e987e
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pyfftw/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def update(self, force=False):
for name, value in os.environ.items():
if name.startswith('PYFFTW_') or name == 'OMP_NUM_THREADS':
new_environ[name] = value
# We update the config variables if at least one NUMBA environment
# We update the config variables if at least one PYFFTW environment
# variable was modified. This lets the user modify values
# directly in the config module without having them when
# reload_config() is called by the compiler.
Expand Down
97 changes: 97 additions & 0 deletions test/test_pyfftw_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@

from pyfftw import config, _threading_type


from .test_pyfftw_base import run_test_suites

import unittest
import os
from numpy.testing import assert_equal


class ConfigTest(unittest.TestCase):

env_keys = ['PYFFTW_NUM_THREADS', 'OMP_NUM_THREADS',
'PYFFTW_PLANNER_EFFORT']
orig_env = {}

def setUp(self):
# store environment variables prior to testing
for key in self.env_keys:
self.orig_env[key] = os.environ.get(key, None)
return

def tearDown(self):
# resstore original environment variables values
for key in self.env_keys:
val = self.orig_env[key]
if val is None:
os.environ.pop(key, None)
else:
os.environ[key] = val
return

def test_default_config(self):
# unset environment variables if they were defined
os.environ.pop('PYFFTW_NUM_THREADS', None)
os.environ.pop('OMP_NUM_THREADS', None)
os.environ.pop('PYFFTW_PLANNER_EFFORT', None)
# defaults to single-threaded and FFTW_ESTIMATE
config.reload_config()
assert_equal(config.NUM_THREADS, 1)
assert_equal(config.PLANNER_EFFORT, 'FFTW_ESTIMATE')

@unittest.skipIf(_threading_type != 'OMP', reason='non-OpenMP build')
def test_default_threads_OpenMP(self):
# unset environment variables if they were defined
os.environ.pop('PYFFTW_NUM_THREADS', None)
os.environ.pop('OMP_NUM_THREADS', None)

# defaults to single-threaded if neither variable is defined
config.reload_config()
assert_equal(config.NUM_THREADS, 1)

# load default from OMP_NUM_THREADS environment variable
os.environ['OMP_NUM_THREADS'] = '2'
config.reload_config()
assert_equal(config.NUM_THREADS, 2)

# PYFFTW_NUM_THREADS overrides OMP_NUM_THREADS when both are defined
os.environ['PYFFTW_NUM_THREADS'] = '4'
config.reload_config()
assert_equal(config.NUM_THREADS, 4)

def test_non_default_config(self):
# set environment variables to non-default values
if _threading_type is None:
os.environ['PYFFTW_NUM_THREADS'] = '1'
else:
os.environ['PYFFTW_NUM_THREADS'] = '4'
os.environ['PYFFTW_PLANNER_EFFORT'] = 'FFTW_MEASURE'

config.reload_config()
assert_equal(config.NUM_THREADS, 4)
assert_equal(config.PLANNER_EFFORT, 'FFTW_MEASURE')

# set values to something else
config.NUM_THREADS = 6
config.PLANNER_EFFORT = 'FFTW_ESTIMATE'

# reload_config should preserve the user-defined values
config.reload_config()
assert_equal(config.NUM_THREADS, 6)
assert_equal(config.PLANNER_EFFORT, 'FFTW_ESTIMATE')

# can reset back to the values from the environment variables
config._env_reloader.reset()
assert_equal(config.NUM_THREADS, 4)
assert_equal(config.PLANNER_EFFORT, 'FFTW_MEASURE')


test_cases = (ConfigTest, )

test_set = None

if __name__ == '__main__':

run_test_suites(test_cases, test_set)

0 comments on commit 34e987e

Please sign in to comment.