run.sh 5.65 KB
Newer Older
Zachary Snow committed
1 2 3 4
#!/bin/bash

clearArtifacts() {
    rm -f one.v two.v
5
    rm -rf dirout
Zachary Snow committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
}

createArtifacts() {
    touch one.v two.v
}

test_prereq() {
    for file in `ls *.sv`; do
        assertConverts "$file"
    done
}

test_default() {
    runAndCapture *.sv
    assertTrue "default conversion should succeed" $result
    assertNotNull "stdout should not be empty" "$stdout"
    assertNull "stderr should be empty" "$stderr"
}

test_stdout() {
    runAndCapture --write=stdout *.sv
    assertTrue "default conversion should succeed" $result
    assertNotNull "stdout should not be empty" "$stdout"
    assertNull "stderr should be empty" "$stderr"
}

test_adjacent() {
    runAndCapture --write=stdout *.sv
    expected="$stdout"

    runAndCapture --write=adjacent *.sv
    assertTrue "adjacent conversion should succeed" $result
    assertNull "stdout should be empty" "$stdout"
    assertNull "stderr should be empty" "$stderr"

    actual=`cat one.v two.v`
    assertEquals "adjacent output should match combined" "$expected" "$actual"
    clearArtifacts
}

test_adjacent_exist() {
    createArtifacts
    runAndCapture --write=adjacent *.sv
49
    assertTrue "adjacent conversion should overwrite and succeed" $result
Zachary Snow committed
50
    assertNull "stdout should be empty" "$stdout"
51 52 53 54 55
    assertNull "stderr should be empty" "$stderr"

    actual=`cat one.v two.v`
    assertEquals "adjacent output should match combined" "$expected" "$actual"
    clearArtifacts
Zachary Snow committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69
}

test_adjacent_extension() {
    createArtifacts
    runAndCapture --write=adjacent *.v
    clearArtifacts

    assertFalse "adjacent conversion should fail" $result
    assertNull "stdout should be empty" "$stdout"
    assertEquals "stderr should list existing files" \
        "Refusing to write adjacent to \"one.v\" because that path does not end in \".sv\"" \
        "$stderr"
}

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
test_file() {
    runAndCapture --write=stdout *.sv
    expected="$stdout"

    rm -f out.v
    runAndCapture --write=out.v *.sv
    assertTrue "file conversion should succeed" $result
    assertNull "stdout should be empty" "$stdout"
    assertNull "stderr should be empty" "$stderr"

    actual=`cat out.v`
    assertEquals "file output should match combined" "$expected" "$actual"
    clearArtifacts
}

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
test_directory() {
    runAndCapture *.sv
    expected="$stdout"

    rm -f dirout/*
    mkdir -p dirout

    runAndCapture --write dirout *.sv
    assertTrue "directory conversion should succeed" $result
    assertNull "stdout should be empty" "$stdout"
    assertNull "stderr should be empty" "$stderr"

    assertTrue "one.v should exist" "[ -f dirout/one.v ]"
    assertTrue "two.v should exist" "[ -f dirout/two.v ]"
    assertTrue "three.v should exist" "[ -f dirout/three.v ]"
100
    assertFalse "P.v should not exist" "[ -f dirout/P.v ]"
101 102 103 104 105 106

    actual=`cat dirout/*.v`
    assertEquals "directory output should match combined" "$expected" "$actual"
    clearArtifacts
}

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
test_directory_pass_through() {
    rm -f dirout/*
    mkdir -p dirout

    runAndCapture --pass-through --write dirout *.sv
    assertTrue "directory conversion should succeed" $result
    assertNull "stdout should be empty" "$stdout"
    assertNull "stderr should be empty" "$stderr"

    assertTrue "one.v should exist" "[ -f dirout/one.v ]"
    assertTrue "two.v should exist" "[ -f dirout/two.v ]"
    assertTrue "three.v should exist" "[ -f dirout/three.v ]"
    assertTrue "P.v should exist" "[ -f dirout/P.v ]"
    clearArtifacts
}

test_directory_directives() {
    module_inp="logic a, b;module example;wire x;endmodule logic c, d;"
    module_out="logic a;
logic b;
module example;
    wire x;
endmodule
logic c;
logic d;"

    check_directory_example "$module_inp" "$module_out"
    check_directory_example "\`default_nettype none\n$module_inp\`resetall" "\`default_nettype none\n$module_out"
    check_directory_example "\`default_nettype none\n\`default_nettype wire\n$module_inp" "\`default_nettype wire\n$module_out"
    check_directory_example "\`celldefine\n$module_inp" "\`celldefine\n$module_out\n\`endcelldefine"
    check_directory_example "\`celldefine\n\`endcelldefine\n$module_inp" "$module_out"
    check_directory_example "\`unconnected_drive pull0\n$module_inp" "\`unconnected_drive pull0\n$module_out\n\`nounconnected_drive"
    check_directory_example "\`unconnected_drive pull0\n\`nounconnected_drive\n$module_inp" "$module_out"
    check_directory_example "\`default_nettype none\n\`celldefine\n\`unconnected_drive pull1\n\`resetall\n$module_inp" "$module_out"
    check_directory_example "\`default_nettype none
\`celldefine
\`unconnected_drive pull1
$module_inp" "\`default_nettype none
\`celldefine
\`unconnected_drive pull1
$module_out
\`nounconnected_drive
\`endcelldefine"
}

check_directory_example() {
    tmp_inp=$SHUNIT_TMPDIR/example.sv
    tmp_out=$SHUNIT_TMPDIR/example.v
    tmp_ref=$SHUNIT_TMPDIR/example_ref.v

    echo -e "$1" > $tmp_inp
    echo -e "$2" | sed -E 's/    /\t/g' > $tmp_ref

    rm -f $tmp_out
    runAndCapture --pass-through --write $SHUNIT_TMPDIR $tmp_inp
    assertTrue "directory conversion should succeed" $result
    assertNull "stdout should be empty: $stdout" "$stdout"
    assertNull "stderr should be empty: $stderr" "$stderr"

    grep -v '//' < $tmp_out > $tmp_out.bak
    mv $tmp_out.bak $tmp_out
    output=`diff --unified $tmp_ref $tmp_out`
    assertTrue "output doesn't match:\n$output" $?
}

172 173 174 175 176
test_unknown() {
    runAndCapture --write=unknown *.sv
    assertFalse "unknown write mode should fail" $result
    assertNull "stdout should be empty" "$stdout"
    assertEquals "stderr should list valid write modes" \
177
        "invalid --write \"unknown\", expected stdout, adjacent, a path ending in .v, or a path to an existing directory" \
178 179 180
        "$stderr"
}

Zachary Snow committed
181 182 183
source ../lib/functions.sh

. shunit2