#!/bin/bash

# Copyright 2015 Divya Kothari <divya.s.kothari@gmail.com>
# Copyright 2015 Vineet Chaturvedi <vinit.chaturvedi@gmail.com> 
# Copyright 2015 Neha Maggu <neha.maggu@gmail.com>

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

kill_yes()
{
  exec 2> /dev/null # To suppress Terminated message after killing
  killall yes
  sleep 1
}

fun_val()
{
  nice yes > /dev/null &
  pid=$!
  nice=`awk '{ print $19 }' /proc/${pid}/stat`
  ppid=`awk '{ print $4 }' /proc/${pid}/stat`
  pnice=`awk '{ print $19 }' /proc/${ppid}/stat`
  exp_nice=`expr $pnice + 10` 
  [ $nice  -eq  $exp_nice ] && echo "yes"
  kill_yes
}

fun_val_n()
{
  nice -n $1 yes > /dev/null &
  pid=$!
  nice=`awk '{ print $19 }' /proc/${pid}/stat`
  echo $nice
  kill_yes
}

testing "nice without nice_value" "fun_val"  "yes\n" "" ""
testing "nice positive nice_value=18" "fun_val_n 18 "  "18\n" "" ""
testing "nice positive nice_value=19" "fun_val_n 19 "  "19\n" "" ""
testing "nice positive nice_value=20" "fun_val_n 20 "  "19\n" "" ""
testing "nice positive nice_value=100" "fun_val_n 100 "  "19\n" "" ""
testing "nice nice_value=0" "fun_val_n 0 "  "0\n" "" ""

# Requires root permissions
if [ "$(id -u)" -ne 0 ]
then
echo "Skipped Negative Nice Value scenarios : NOT ROOT"
continue 2>/dev/null
exit
fi

testing "nice negative nice_value=-5" "fun_val_n -5 "  "-5\n" "" ""
testing "nice negative nice_value=-20" "fun_val_n -20 "  "-20\n" "" ""
testing "nice negative nice_value=-25" "fun_val_n -25 "  "-20\n" "" ""

