set test_name array_slicing

# basic version of the script to be used. values need to be subbed in
# for each test
set script { \
  global val; \
  probe oneshot { \
    val[$1, $2] = $3; val[$4,$5] = $6 \
    print("systemtap starting probe\nsystemtap ending probe\n"); \
    foreach (a=[b,c] in val[$7,$8]) {print(a)} \
    print(" end") \
  } \
}

# this is set up to act like a normal for loop
stap_run "$test_name foreach (... val\[*, *\])" "" "11 end" -e $script 2 {"hello"} 1 1 {"hey"} 1 * *

# testing foreach( a=[b,c] in val[int, int] )
stap_run "$test_name foreach (... val\[int, int\])" "" "2 end"  -e $script 2 2 2 3 3 3 2 2

# testing foreach( a=[b,c] in val[string, string] ), where there is a match
stap_run "$test_name foreach (... val\[string, string\])" "" "1 end"  -e $script {"asdf"} {"jkl"} 1 {"fdsa"} {"lkj"} 2 {"asdf"} {"jkl"}

# testing foreach( a=[b,c] in val[variable, *] ), where there is a match
stap_run "$test_name foreach (... val\[variable, *\])" "" "4 end"  -e $script 9 1 4 8 9 9 {val[8,9]} *

# testing foreach( a=[b,c] in val[string, variable/string] ), where
# there is a match
stap_run "$test_name foreach (... val\[variable, string\])" "" "asdf end"  -e $script {"hi"} {"hello"} {"asdf"} {"hi"} {"hi"} {"hello"} {"hi"} {val["hi","hi"]}

# testing foreach(... val[c,d]){c++;d++}
# in this case, expecting the c++ and d++ to not affect the c and d in
# the array slice
set script { \
  global val, c=1, d=3; \
  probe oneshot { \
    print("systemtap starting probe\nsystemtap ending probe\n"); \
    val[1,3]=5; val[2,3]=6; \
    foreach ([a,b] in val[c,d]){print(val[a,b]);c++;d++} \
  } \
}
stap_run "$test_name foreach (... val\[c,d\]) {c++;d++;}" "" "5" -e $script

# testing foreach(.. val[expression, *])
set script { \
  global val, c=1, d=2; \
  probe oneshot { \
    print("systemtap starting probe\nsystemtap ending probe\n"); \
    val[1,3]=5; val[2,3]=6; \
    foreach ([a,b] in val[(c==d ? d : c), *]){print(val[a,b])} \
  } \
}
stap_run "$test_name foreach (... val\[expression,*\])" "" "5" -e $script

# testing sorting in foreach loops
set script { \
  global val, stats \
  probe oneshot { \
    val[1, 1] = 1; val[3, 3] = 5; val[1, 2] = 2; val[2, 2] = 4; val[2, 1] = 3; \
    stats [1, 1] <<< 9; \
    stats [2, 1] <<< 2; stats [2, 1] <<< 6; \
    stats [23, 1] <<< 5; \
    print("systemtap starting probe\nsystemtap ending probe\n"); \
    foreach (c=[a, b] in val[*,*]+) print(c); \
    foreach (c=[a, b] in val[*,*]-) print(c); \
    foreach ([a,b] in stats[*,1] @sum-) print(@sum(stats[a,b])); \
  } \
}
stap_run "$test_name foreach sorting" "" "1234554321985" -e $script


# testing array slicing with delete statements
set script { \
  global val; \
  probe oneshot { \
    val[9, "hello"] = 9; val[300,"there"] = 6; \
    print("systemtap starting probe\nsystemtap ending probe\n"); \
    delete val[$1, $2]; \
    print(val[9, "hello"]);print(val[300, "there"]); exit(); \
  } \
}

# testing delete val[*,*]
stap_run "$test_name delete val\[*,*\]" "" "00" -e $script * *

# testing delete val[*, string]
stap_run "$test_name delete val\[*, string\]" "" "06" -e $script * {"hello"}

# testing delete val[int,*]
stap_run "$test_name delete val\[int, *\]" "" "90" -e $script 300 *

# testing delete val[expression,*]
stap_run "$test_name delete val\[epression, *\]" "" "90" -e $script {(val[9,"hello"] == val[300,"there"] ? 9 : 300)} *

# testing delete statements on pmaps
set script { \
  global stats \
  probe oneshot { \
    stats [1, 1] <<< 9; stats [1, 1] <<< 2; stats [1, 1] <<< 6; \
    stats [2, 2] <<< 1; \
    delete stats [1, *]; \
    print("systemtap starting probe\nsystemtap ending probe\n"); \
    print (@count(stats[1, 1])); print(@count(stats[2,2])); \
  } \
}
stap_run "$test_name delete pmaps" "" "01" -e $script

# testing membership, [*, *] in foo
set script { \
  global val; \
  probe oneshot { \
    val[9,"hello"] = 9; val[300,"there"] = 6; \
    print("systemtap starting probe\nsystemtap ending probe\n"); \
    if ([$1, $2] in val) { print("in");} else {print ("not in");} \
  } \
}
# testing membership [*,*]
stap_run "$test_name membership \[*,*\] in val" "" "in" -e $script * *

# testing membership [*, string]
stap_run "$test_name membership \[*, string\] in val" "" "in" -e $script * {"hello"}

# testing membership [int,*]
stap_run "$test_name membership \[int, *\] in val" "" "not in" -e $script 309 *

# testing membership [expression,*]
stap_run "$test_name membership \[expression,*\] in val" "" "not in" -e $script {(val[9, "hello"] == val[300, "there"]? 9 : 900)} *

# testing membership with pmaps
set script { global stats \
  probe begin {stats[1,1] <<< 2; exit();} \
  probe end { \
    print("systemtap starting probe\nsystemtap ending probe\n"); \
    print([1, *] in stats); print([2, *] in stats) \
  } \
}
stap_run "$test_name membership pmaps" "" "10" -e $script

# testing membership with pmaps, to make sure it doesn't re-aggregate
# in the loop
set script { global stats \
  probe begin {stats[1,1] <<< 2; exit();} \
  probe end { \
    print("systemtap starting probe\nsystemtap ending probe\n"); \
    foreach([x,y] in stats) print([1,*] in stats) \
  } \
}
stap_run "$test_name membership pmaps (2)" "" "1" -e $script
