github.com/eth-easl/loader@v0.0.0-20230908084258-8a37e1d94279/tools/trace_synthesizer/tests/synthesize_test.go (about)

     1  /*
     2   * MIT License
     3   *
     4   * Copyright (c) 2023 EASL and the vHive community
     5   *
     6   * Permission is hereby granted, free of charge, to any person obtaining a copy
     7   * of this software and associated documentation files (the "Software"), to deal
     8   * in the Software without restriction, including without limitation the rights
     9   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    10   * copies of the Software, and to permit persons to whom the Software is
    11   * furnished to do so, subject to the following conditions:
    12   *
    13   * The above copyright notice and this permission notice shall be included in all
    14   * copies or substantial portions of the Software.
    15   *
    16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    22   * SOFTWARE.
    23   */
    24  
    25  package main
    26  
    27  import (
    28  	"encoding/csv"
    29  	"fmt"
    30  	log "github.com/sirupsen/logrus"
    31  	"github.com/stretchr/testify/assert"
    32  	"os"
    33  	"os/exec"
    34  	"strconv"
    35  	"testing"
    36  )
    37  
    38  func TestSynthesizer(t *testing.T) {
    39  	err := os.Chdir("..")
    40  	if err != nil {
    41  		log.Fatalf("Couldn't change directory: %s", err)
    42  	}
    43  	cmd := exec.Command("python3", "generate_test.py")
    44  	output, err := cmd.CombinedOutput()
    45  	if err != nil {
    46  		fmt.Println(fmt.Sprint(err) + ": " + string(output))
    47  	}
    48  	rows := readInvocations("test_output/invocations.csv")
    49  	sum := calculate(rows)
    50  	assert.Equal(t, 16200, sum)
    51  }
    52  
    53  func readInvocations(name string) [][]string {
    54  	f, err := os.Open(name)
    55  	if err != nil {
    56  		log.Fatal("Cannot open test output")
    57  	}
    58  
    59  	defer f.Close()
    60  	r := csv.NewReader(f)
    61  	rows, err := r.ReadAll()
    62  	if err != nil {
    63  		log.Fatal("Cannot read CSV data:", err.Error())
    64  	}
    65  
    66  	return rows
    67  }
    68  
    69  func calculate(rows [][]string) int {
    70  	sum := 0
    71  	for i := range rows {
    72  		if i == 0 {
    73  			continue
    74  		}
    75  		for j := range rows[i] {
    76  			if j == 0 || j == 1 || j == 2 {
    77  				continue
    78  			}
    79  			v, err := strconv.Atoi(rows[i][j])
    80  			if err != nil {
    81  				log.Fatalf("Couldn't convert to integer: %s", err)
    82  			}
    83  			sum += v
    84  
    85  		}
    86  	}
    87  
    88  	return sum
    89  }