#!/bin/bash

# Copyright 2015 Neha Maggu <maggu.neha@gmail.com>

[ -f testing.sh ] && . testing.sh

#testing "name" "command" "result" "infile" "stdin"

#When Input and output strings are of same size 
L1="neha\narohi\n"  L2="bbb\n"
testing "tr with same file name" "tr abc 123 " "neh1\n1rohi\n222\n"  "" "$L1$L2"

#When input string is special character
L1="speci*l\n"  L2="%*ll\n"  L3="!hara!ter\n"
testing "Input string is special character" "tr *%! abc" "special\nball\ncharacter\n" "" "$L1$L2$L3"

#When first string is bigger than second string
L1="NEHA\n"  L2="EMPTY\n"  L3="1234\n"  L4="SENHA\n"
testing "First string is of greater length" "tr NEHA ABC " "ABCC\nBMPTY\n1234\nSBACC\n" "" "$L1$L2$L3$L4"

#When first string is smaller than second string
L1="small\n" L2="big length\n"  L3="length\n"  L4="coorporate culture\n"
testing "First string is of smaller length" "tr small length " \
  "lentt\nbig tength\ntength\ncoorpornte cutture\n" "" "$L1$L2$L3$L4"

#When First string is empty
L1="Second string\n"
testing "First string is empty" "tr "" abc " "Second string\n" "" "$L1"

#When Second string is empty
testing "Second string is empty" " status=$(tr ABC "" 2>/dev/null ;echo $?) && echo yes "  "yes\n" "" ""

#Multiple strings as arguments
testing "tr with multiple arguments" " status=$(tr first seconf third 2>/dev/null ;echo $?) && \
  echo yes "  "yes\n" "" ""

#Converting Lowercase into upercase string
L1="Lower\n" L2="abcdefghijkl\n" L3="Upper\n"
testing "Lower to Uppercase-1" "tr lower LOWER " "LOWER\n"  "" "$L1"

testing "Lower to Uppercase-2" "tr [:lower:] [:upper:]" "ABCDEFGHIJKL\n" "" "$L2"

testing "Lower to uppercase-3" "tr a-z A-Z" "UPPER\n" "" "$L3"

#Replacing string with mulptle character
L1="abcdefgh\n"
testing "Replace string with multiple character" " tr a-h jklmnopq " "jklmnopq\n" "" "$L1"

#Delete character from string
L1="remove\n"  L2="delete\n"  L3="erase\n" L4="verified\n"
testing "Delete character from the string" " tr -d delete " "rmov\n\nras\nvrifi\n" "" "$L1$L2$L3$L4"

#tr command with special symbols

#Delete space from string
L1="I-   N      E       E   D -   S  P  A  C  E     \n "
testing "Delete spaces from string " " tr -d \" \" " "I-NEED-SPACE\n" "" "$L1"

#Delete tabs spacing
L1="tes     ting\n"
testing "Delete tab from the string " " tr -d \t " "es     ing\n" "" "$L1"

#Translate braces into parenthesis in file
echo -ne "{Translating Curly braces into parenthesis}\n" > inputfile
testing "Translate braces into parenthesis in file" " tr '{}' '()' < inputfile " \
  "(Translating Curly braces into parenthesis)\n" "" ""
rm -rf inputfile 

#Squeeze repetition of characters 
L1="This  is  for   testing "
testing "Squeeze repetition of characters "  "tr -s [:space:] ' ' " "This is for testing " ""  "$L1" 

#Complement the sets using -c option
L1="my username is 432234\n"
testing "Complement the sets using -c option" "tr -cd [:digit:]" "432234" "" "$L1" 

#To remove alphanumeric character with "C"
testing "Remove alphanumeric character" "tr -Cd [:alpha:]" "myusernameis" "" "$L1"

#Join all the lines in a file into a single line
L1="Join all the lines into \n Single Line\n"
testing "Join all the lines into a single line" "tr -s '\n' ' ' " \
  "Join all the lines into Single Line " "" "$L1"

#Remove all "diacritical" marks 
L1="marke\n" L2="Remove\n" L3="eafe\n" 
testing "Remove all diacritical  marks " " tr [=e=] s" "marks\nRsmovs\nsafs\n" "" "$L1$L2$L3"

#Escape sequence
L1="This is for testing\n"
testing "Escape sequence" "tr [:space:] \t " "Thististforttestingt" "" "$L1"

#Remove digits
L1="my username is 432234neham\n"
testing "Remove digits" " tr -d [:digit:]" "my username is neham\n" "" "$L1"


L1="the geek stuff\n"
testing "Remove letter with option d " " tr -d t" "he geek suff\n" "" "$L1"


#Remove characters except digits
L1="my username is 432234"
testing "Remove characters except digits" " tr -cd [:digit:]" "432234" "" "$L1"

#Translate file contents
echo -ne "This is file\n" > file
testing "Translate contents of file to uppercase"  "tr "[:lower:]" "[:upper:]" < file" "THIS IS FILE\n" "" ""
rm -rf file

#Class Xdigit
L1="21312312\n!#$!@#$!!@#\n0x23123"
testing "Delete special characters with Xdigit class" " tr -cd '[:xdigit:]' " "21312312023123" "" "$L1"

