Created
October 8, 2017 07:00
-
-
Save bassemZohdy/407a71971d2e66fc59a5c7a408266570 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -e | |
#set -x | |
arrayContains(){ | |
ARRAY=($1); | |
ITEM=$2; | |
if echo "${ARRAY[*]}" | grep -E -q " $ITEM |^$ITEM | $ITEM\$" ; | |
then | |
echo true; | |
else | |
echo false; | |
fi; | |
} | |
#Test Cases | |
PASS=0 | |
TEST_INDEX=1 | |
if [ "$(arrayContains 'aaa bbb ccc ddd eee' abc)" == true ]; then echo "$TEST_INDEX faild"; ((PASS++));fi | |
(( TEST_INDEX++)) | |
if [ "$(arrayContains 'aaa bbb ccc ddd eee' aaa)" == false ]; then echo "$TEST_INDEX faild"; ((PASS++));fi | |
(( TEST_INDEX++)) | |
if [ "$(arrayContains 'aaa bbb ccc ddd eee' bbb)" == false ]; then echo "$TEST_INDEX faild"; ((PASS++));fi | |
(( TEST_INDEX++)) | |
if [ "$(arrayContains 'aaa bbb ccc ddd eee' ccc)" == false ]; then echo "$TEST_INDEX faild"; ((PASS++));fi | |
(( TEST_INDEX++)) | |
if [ "$(arrayContains 'aaa bbb ccc ddd eee' ddd)" == false ]; then echo "$TEST_INDEX faild"; ((PASS++));fi | |
(( TEST_INDEX++)) | |
if [ "$(arrayContains 'aaa bbb ccc ddd eee' eee)" == false ]; then echo "$TEST_INDEX faild"; ((PASS++));fi | |
(( TEST_INDEX++)) | |
if [ "$(arrayContains 'aaa bbb ccc ddd eee' fff)" == true ]; then echo "$TEST_INDEX faild"; ((PASS++));fi | |
(( TEST_INDEX++)) | |
if [ "$(arrayContains 'aaa bbb ccc ddd eee' a)" == true ]; then echo "$TEST_INDEX faild"; ((PASS++));fi | |
(( TEST_INDEX++)) | |
if [ "$(arrayContains 'aaa bbb ccc ddd eee' aa)" == true ]; then echo "$TEST_INDEX faild"; ((PASS++));fi | |
(( TEST_INDEX++)) | |
if [ "$(arrayContains 'aaa bbb ccc ddd eee' ' eee')" == true ]; then echo "$TEST_INDEX faild"; ((PASS++));fi | |
(( TEST_INDEX++)) | |
if [ "$(arrayContains 'aaa bbb ccc ddd eee' 'aaa ')" == true ]; then echo "$TEST_INDEX faild"; ((PASS++));fi | |
if [[ $PASS -gt 0 ]]; then | |
echo "FAILD $PASS from $TEST_INDEX" | |
else | |
echo "PASS all of $TEST_INDEX" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment