Skip to content

Commit 91f8fd1

Browse files
author
Damian Rouson
committed
Add tests for the stack.sh script
1 parent fba06a0 commit 91f8fd1

File tree

4 files changed

+310
-4
lines changed

4 files changed

+310
-4
lines changed

prerequisites/stack.sh

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function stack_destroy
5151
function stack_push
5252
{
5353
: "${1?'Missing stack name'}"
54-
: "${2?'Missing item(s) to push'}"
54+
: "${2?'Missing variable name in stack_push'}"
5555

5656
if no_such_stack "$1"
5757
then
@@ -125,16 +125,16 @@ function no_such_stack
125125
function stack_pop
126126
{
127127
: "${1?'Missing stack name'}"
128-
: "${2?'Missing name of variable for popped result'}"
129-
130-
eval 'let _i=$'"_stack_$1_i"
128+
: "${2?'Missing variable name in stack_pop'}"
131129

132130
if no_such_stack "$1"
133131
then
134132
echo "No such stack -- $1" >&2
135133
return 1
136134
fi
137135

136+
eval 'let _i=$'"_stack_$1_i"
137+
138138
if [[ "$_i" -eq 0 ]]
139139
then
140140
echo "Empty stack -- $1" >&2
@@ -247,6 +247,9 @@ function stack_exists
247247
# echo "Got $top"
248248
function stack_peek
249249
{
250+
: "${1?'Missing stack name'}"
251+
: "${2?'Missing variable name in stack_peek'}"
252+
250253
stack_pop "$1" "$2"
251254
eval argument_name="\$$2"
252255
# shellcheck disable=SC2154
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#!/usr/bin/env bash
2+
#
3+
# installation-scripts.sh
4+
#
5+
# -- This script tests the bash scripts that install OpenCoarrays and its prerequisites.
6+
#
7+
# OpenCoarrays is distributed under the OSI-approved BSD 3-clause License:
8+
# Copyright (c) 2015-2016, Sourcery, Inc.
9+
# Copyright (c) 2015-2016, Sourcery Institute
10+
# All rights reserved.
11+
#
12+
# Redistribution and use in source and binary forms, with or without modification,
13+
# are permitted provided that the following conditions are met:
14+
#
15+
# 1. Redistributions of source code must retain the above copyright notice, this
16+
# list of conditions and the following disclaimer.
17+
# 2. Redistributions in binary form must reproduce the above copyright notice, this
18+
# list of conditions and the following disclaimer in the documentation and/or
19+
# other materials provided with the distribution.
20+
# 3. Neither the names of the copyright holders nor the names of their contributors
21+
# may be used to endorse or promote products derived from this software without
22+
# specific prior written permission.
23+
#
24+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
25+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27+
# IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28+
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29+
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30+
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31+
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33+
# POSSIBILITY OF SUCH DAMAGE.
34+
#
35+
# Portions of this script derive from BASH3 Boilerplate and are distributed under
36+
# the following license:
37+
#
38+
# The MIT License (MIT)
39+
#
40+
# Copyright (c) 2014 Kevin van Zonneveld
41+
#
42+
# Permission is hereby granted, free of charge, to any person obtaining a copy
43+
# of this software and associated documentation files (the "Software"), to deal
44+
# in the Software without restriction, including without limitation the rights
45+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
46+
# copies of the Software, and to permit persons to whom the Software is
47+
# furnished to do so, subject to the following conditions:
48+
#
49+
# The above copyright notice and this permission notice shall be included in all
50+
# copies or substantial portions of the Software.
51+
#
52+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
53+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
54+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
55+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
56+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
57+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
58+
# SOFTWARE.
59+
#
60+
#
61+
# - https://github.com/kvz/bash3boilerplate
62+
# - http://kvz.io/blog/2013/02/26/introducing-bash3boilerplate/
63+
#
64+
# Version: 2.0.0
65+
#
66+
# Authors:
67+
#
68+
# - Kevin van Zonneveld (http://kvz.io)
69+
# - Izaak Beekman (https://izaakbeekman.com/)
70+
# - Alexander Rathai ([email protected])
71+
# - Dr. Damian Rouson (http://www.sourceryinstitute.org/) (documentation)
72+
#
73+
# Licensed under MIT
74+
# Copyright (c) 2013 Kevin van Zonneveld (http://kvz.io)
75+
76+
# The invocation of bootstrap.sh below performs the following tasks:
77+
# (1) Import several bash3boilerplate helper functions & default settings.
78+
# (2) Set several variables describing the current file and its usage page.
79+
# (3) Parse the usage information (default usage file name: current file's name with -usage appended).
80+
# (4) Parse the command line using the usage information.
81+
82+
### Start of boilerplate -- do not edit this block #######################
83+
export OPENCOARRAYS_SRC_DIR="${OPENCOARRAYS_SRC_DIR:-${PWD%/}/../../..}"
84+
if [[ ! -f "${OPENCOARRAYS_SRC_DIR}/src/libcaf.h" ]]; then
85+
echo "Please run this script inside the top-level OpenCoarrays source directory or "
86+
echo "set OPENCOARRAYS_SRC_DIR to the OpenCoarrays source directory path."
87+
exit 1
88+
fi
89+
export B3B_USE_CASE="${B3B_USE_CASE:-${OPENCOARRAYS_SRC_DIR}/prerequisites/use-case}"
90+
if [[ ! -f "${B3B_USE_CASE:-}/bootstrap.sh" ]]; then
91+
echo "Please set B3B_USE_CASE to the bash3boilerplate use-case directory path."
92+
exit 2
93+
else
94+
# shellcheck source=./prerequisites/use-case/bootstrap.sh
95+
source "${B3B_USE_CASE}/bootstrap.sh" "$@"
96+
fi
97+
### End of boilerplate -- start user edits below #########################
98+
99+
# Set expected value of present flags that take no arguments
100+
export __flag_present=1
101+
102+
# Set up a function to call when receiving an EXIT signal to do some cleanup. Remove if
103+
# not needed. Other signals can be trapped too, like SIGINT and SIGTERM.
104+
function cleanup_before_exit () {
105+
info "Cleaning up. Done"
106+
}
107+
trap cleanup_before_exit EXIT # The signal is specified here. Could be SIGINT, SIGTERM etc.
108+
109+
110+
pushd "${OPENCOARRAYS_SRC_DIR}"/src/tests/installation
111+
112+
source "${OPENCOARRAYS_SRC_DIR}"/prerequisites/stack.sh
113+
source test-stack.sh
114+
test_stack
115+
116+
popd
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
-b --install-branch [arg] Install the specified repository development branch.
2+
-B --list-branches [arg] List the available branches in the specified package's repository.
3+
-c --with-c [arg] Use specified C compiler.
4+
-C --with-cxx [arg] Use specified C++ compiler.
5+
-d --debug Enable debug mode.
6+
-D --print-downloader [arg] Print download program for package specified in argument.
7+
-e --verbose Enable verbose mode, print script as it is executed.
8+
-f --with-fortran [arg] Use specified Fortran compiler.
9+
-h --help Print this page.
10+
-i --install-prefix [arg] Install package in specified path. Default="${OPENCOARRAYS_SRC_DIR}/prerequisites/installations/${package_name:-}/${version_to_build:-}"
11+
-I --install-version [arg] Install package version.
12+
-j --num-threads [arg] Number of threads to use when invoking make. Default="1"
13+
-l --list-packages Print packages this script can install.
14+
-m --with-cmake [arg] Use specified CMake installation. Default="cmake"
15+
-M --with-mpi [arg] Use specified MPI installation.
16+
-n --no-color Disable color output.
17+
-p --package [arg] Install specified package. Default="opencoarrays"
18+
-P --print-path [arg] Print installation directory for specified package.
19+
-U --print-url [arg] Print download location for specified package.
20+
-v --version Print OpenCoarrays version number.
21+
-V --print-version [arg] Print version number for specified package.
22+
-y --yes-to-all Build non-interactively by assuming affirmative user responses.
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
functions_exist()
2+
{
3+
: "${1?'Missing function name list'}"
4+
5+
for function_name
6+
do
7+
if [[ "`type -t $function_name`" != "function" ]]; then
8+
emergency "function ${function_name} does not exist"
9+
fi
10+
done
11+
}
12+
13+
detect_missing_variable_name()
14+
{
15+
: "${1?'detect_missing_variable name: Missing function name'}"
16+
17+
stack_new __dummy_stack
18+
19+
for function_name
20+
do
21+
expected_error="Missing variable name in ${function_name}"
22+
error_message="$($function_name _dummy_stack_name 2>&1 > /dev/null)" || true
23+
error_message="${error_message##*: }" # grab text after final colon/space sequence
24+
if [[ "${error_message}" != ${expected_error} ]]; then
25+
emergency "$function_name failed to detect '${expected_error}'"
26+
fi
27+
done
28+
stack_destroy __dummy_stack
29+
}
30+
31+
detect_missing_stack_name()
32+
{
33+
: "${1?'detect_missing_name: Missing function name'}"
34+
35+
expected_error="Missing stack name"
36+
debug "detect_missing_stack_name: \${expected_error}=${expected_error}"
37+
38+
for function_name
39+
do
40+
debug "detect_missing_stack_name: executing \"\$($function_name 2>&1 > /dev/null)\" || true"
41+
error_message="$($function_name 2>&1 > /dev/null)" || true
42+
error_message="${error_message##*: }" # grab text after final colon/space sequence
43+
debug "detect_missing_stack_name: \${error_message}=${error_message}"
44+
if [[ "${error_message}" != ${expected_error} ]]; then
45+
emergency "$function_name failed to detect '${expected_error}'"
46+
fi
47+
done
48+
}
49+
50+
detect_no_such_stack()
51+
{
52+
: "${1?'detect_no_such_name: Missing function name'}"
53+
54+
stack_name="__nonexistent_stack"
55+
expected_error="No such stack -- "
56+
debug "detect_no_such_stack: \${expected_error}=${expected_error}"
57+
58+
dummy_variable=""
59+
60+
for function_name
61+
do
62+
debug "detect_no_such_stack: executing \"\$($function_name $stack_name dummy_variable 2>&1 > /dev/null)\" || true"
63+
error_message="$($function_name $stack_name dummy_variable 2>&1 > /dev/null)" || true
64+
missing_stack="${error_message##${expected_error}}" # grab text after final colon/space sequence
65+
debug "detect_no_such_stack: \${missing_stack}=${missing_stack}"
66+
if [[ "${missing_stack}" != ${stack_name} ]]; then
67+
emergency "$function_name failed to detect missing '${stack_name}'"
68+
fi
69+
done
70+
}
71+
72+
detect_duplicate_stack_creation()
73+
{
74+
: "${1?'detect_duplicate_stack_creation: Missing function name'}"
75+
76+
stack_name="__dummy_stack"
77+
stack_new "${stack_name}"
78+
79+
expected_error="Stack already exists -- "
80+
debug "detect_duplicate_stack_creation: \${expected_error}=${expected_error}"
81+
function_name="stack_new"
82+
83+
debug "detect_duplicate_stack_creation: executing \"\$($function_name $stack_name 2>&1 > /dev/null)\" || true"
84+
error_message="$($function_name $stack_name dummy_variable 2>&1 > /dev/null)" || true
85+
duplicate_stack="${error_message##${expected_error}}" # grab text after final colon/space sequence
86+
debug "detect_no_such_stack: \${duplicate_stack}=${duplicate_stack}"
87+
if [[ "${duplicate_stack}" != ${stack_name} ]]; then
88+
emergency "$function_name failed to detect duplicate '${stack_name}'"
89+
fi
90+
stack_destroy ${stack_name}
91+
}
92+
93+
verify_stack_size_changes()
94+
{
95+
stack_new foobar
96+
stack_push foobar kernel
97+
stack_size foobar foobar_size
98+
if [[ "${foobar_size}" != "1" ]]; then
99+
emergency "verify_stack_size_changes: size=${foobar_size} (expected 1)"
100+
fi
101+
102+
stack_peek foobar tmp
103+
if [[ "${tmp}" != "kernel" ]]; then
104+
emergency "verify_stack_size_changes: peeked item ('${tmp}') mismatch with pushed item ('kernel')"
105+
fi
106+
107+
stack_size foobar should_be_unchanged
108+
if [[ "${should_be_unchanged}" != "1" ]]; then
109+
emergency "verify_stack_size_changes: size=${should_be_unchanged} (expected 1)"
110+
fi
111+
112+
stack_pop foobar popped
113+
if [[ "${popped}" != "kernel" ]]; then
114+
emergency "verify_stack_size_changes: popped item ('${popped}') mismatch with pushed item ('kernel')"
115+
fi
116+
117+
stack_size foobar new_foobar_size
118+
if [[ "${new_foobar_size}" != "0" ]]; then
119+
emergency "verify_stack_size_changes: size=${foobar_size} (expected 0)"
120+
fi
121+
}
122+
123+
test_stack()
124+
{
125+
# Verify availability of expected functions:
126+
functions_exist \
127+
stack_new stack_exists stack_print stack_push stack_peek stack_pop stack_size stack_destroy
128+
129+
# Verify that each named function detects missing stack-name arguments:
130+
detect_missing_stack_name \
131+
stack_new stack_exists stack_print stack_push stack_peek stack_pop stack_size stack_destroy
132+
133+
# Verify that each named function detects missing names of the variable to push, pop, or peek:
134+
detect_missing_variable_name \
135+
stack_pop stack_push stack_peek
136+
137+
# Verify that each named function detects non-existent stacks:
138+
detect_no_such_stack \
139+
stack_print stack_pop stack_push stack_size
140+
141+
# Verify that duplicate creation generates the expected error:
142+
detect_duplicate_stack_creation \
143+
stack_new
144+
145+
# Verify that push, peek, and pop yield correct size changes or lack thereof:
146+
verify_stack_size_changes
147+
148+
emergency "finito but some failing tests were not run (see ${OPENCOARRAYS_SRC_DIR}/src/tests/installation/test-stack.sh)"
149+
150+
# Failing tests:
151+
152+
{ # fail to detect non-existent stack
153+
detect_no_such_stack \
154+
stack_peek
155+
detect_no_such_stack \
156+
stack_exists
157+
detect_no_such_stack \
158+
stack_destroy
159+
}
160+
161+
{ # error on checking size of newly created
162+
stack_new foo
163+
stack_size foo foo_size
164+
}
165+
}

0 commit comments

Comments
 (0)