EmanueleManca

Untitled

Feb 6th, 2021
633
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "io/ioutil"
  6.     "os"
  7.     "strings"
  8.     "testing"
  9. )
  10.  
  11. func TestFiltro(t *testing.T) {
  12.     RunTest(t, "7 15 3", "63")
  13.     RunTest(t, "3 5 2", "8")
  14.     RunTest(t, "11 19 4", "107")
  15.     RunTest(t, "2 6 2", "8")
  16.     RunTest(t, "1 10 2", "25")
  17.  
  18. }
  19.  
  20. func RunTest(t *testing.T, input string, result string) {
  21.     tempFile, err := ioutil.TempFile("", "out")
  22.     if err != nil {
  23.         t.Error("Error while creating temporary file")
  24.     }
  25.     defer tempFile.Close()
  26.  
  27.     tempFile, os.Stdout = os.Stdout, tempFile
  28.  
  29.     os.Args = append([]string{"esercizio_filtro.go"}, strings.Split(input, " ")...)
  30.  
  31.     main()
  32.  
  33.     _, err = os.Stdout.Seek(0, 0)
  34.     if err != nil {
  35.         t.Error("Error while resetting file pointer temporary file")
  36.     }
  37.     output, err := ioutil.ReadAll(os.Stdout)
  38.     if err != nil {
  39.         t.Error("Error while reading output temporary file")
  40.     }
  41.     if tOut, tRes := strings.Trim(string(output), "\n"), strings.Trim(result, "\n"); tOut != tRes {
  42.         t.Error(fmt.Sprintf("\nCodifica compressa stringa: %s\nExpected result: <%s>\nGiven result: <%s>\n", strings.Join(os.Args[1:], " "), tRes, tOut))
  43.     }
  44.  
  45.     tempFile, os.Stdout = os.Stdout, tempFile
  46. }
  47.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×