bringing in testkit tools
[feisty_meow.git] / testkit / library / random_ids_manager.sh
1 #!/bin/bash
2
3 # an unfinished idea for how to manage identifiers that are randomly
4 # generated but are also uniquely identified for later use.
5 #
6 # Author: Chris Koeritz
7
8 export RANDOM_IDS=()
9
10 # given a name for a randomly assigned number, this will generate a
11 # new random value and add it to the RANDOM_IDS list.  it is an error
12 # to try to change an existing random id, because the id may already have
13 # been used in file generation and so forth.
14 function setup_random_id()
15 {
16   name="$1"; shift
17   if [ ! -z "${RANDOM_IDS[$name]}" ]; then
18     echo "FAILURE: trying to reassign already generated random id for '$name'"
19     return 1
20   fi
21   new_id=$RANDOM-$RANDOM-$RANDOM
22   RANDOM_IDS[$name]=$new_id
23   return 0
24 }
25
26 # returns the random value assigned under the name.  it is an error
27 # to request one that does not exist yet; this implies the test has
28 # not properly configured the random ids it will use.
29 function get_random_id()
30 {
31   name="$1"; shift
32   if [ -z "${RANDOM_IDS[$name]}" ]; then
33     echo "FAILURE-to-find-$name"
34     return 1
35   fi
36   echo "${RANDOM_IDS[$name]}"
37   return 0
38 }
39
40 ## 
41 ## # test suite
42 ## setup_random_id "george"
43 ## if [ $? -ne 0 ]; then echo TEST failed to set george; fi
44 ## setup_random_id "lucy"
45 ## if [ $? -ne 0 ]; then echo TEST failed to set lucy; fi
46 ## echo "lucy's id is: $(get_random_id lucy)" 
47 ## lucy_id=$(get_random_id lucy)=
48 ## if [ $? -ne 0 ]; then echo TEST failed to get lucy; fi
49 ## echo "george's id is: $(get_random_id george)"
50 ## george_id=$(get_random_id george)
51 ## if [ $? -ne 0 ]; then echo TEST failed to get george; fi
52 ## 
53 ## setup_random_id "george" &>/dev/null
54 ## if [ $? -eq 0 ]; then echo TEST failed to trap george being reset; fi
55 ## setup_random_id "lucy" &>/dev/null
56 ## if [ $? -eq 0 ]; then echo TEST failed to trap lucy being reset; fi
57 ## get_random_id tony
58 ## if [ $? -eq 0 ]; then echo TEST failed to trap non-existent id request; fi
59 ## 
60
61