Skip to content
Snippets Groups Projects
Commit 0aa80c63 authored by zaglanz's avatar zaglanz
Browse files

to test gcov on WSL

parent 2dde0576
No related branches found
No related tags found
2 merge requests!2Merge for work. Discard merge branch.,!1Code not finished, but runnable. Use it as a base for work.
This commit is part of merge request !2. Comments created here will be created in the context of that merge request.
Showing
with 1059 additions and 0 deletions
File added
File added
File added
Zork85 @ 95b1fd1f
Subproject commit 95b1fd1feead1d52ce079678070f37daaf66766d
#! /usr/bin/sh
# Script to run the program, generate its coverage information
# and move it to a directory
# Make directory to save the output
mkdir -p Temporary
GAN_Inputs=(./GanSamples/*.txt)
Total=${#GAN_Inputs[@]}
echo Total $Total
GAME=0
for input in "${GAN_Inputs[@]}"; do
# Start fresh
GAME=$(( GAME +1 ))
echo Processing $GAME with $input
cp -r zork Temporary/Zork$GAME
cp $input Temporary/Zork$GAME/GanInput
cd Temporary/Zork$GAME
#make -s
touch rooms.c.gcov
touch nrooms.c.gcov
tr -d '\15\32' < GanInput > UnixGanInput
../.././zork/zork.exe < UnixGanInput &> ZorkOut.gameOut
# Generate the coverage information
fd -d 1 -e gcda -X gcovr > /dev/null
# Make directory to save the output
mkdir -p ../.././GanCoverage/Game$GAME/
cp UnixGanInput ../.././GanCoverage/Game$GAME/input
fd -d 1 -e gcov -e gameOut -X mv {} ../.././GanCoverage/Game$GAME
cd ../../
rm -rf ./Temporary/Zork$GAME
done
rmdir Temporary
#! /usr/bin/sh
# Script to run the program, generate its coverage information
# and move it to a directory
# Make directory to save the output
GAME=${1?Error:Game number tag not provided}
# Start fresh
cp -r zork Temporary/Zork$GAME
cd Temporary/Zork$GAME
make -s
# Populate some commands from the game
zorkCommands=(
"east" "west" "north" "south" "up" "down"
"inventory" "diagnose" "take all" "drop all"
"kill troll with sword" "kill thief with knife"
"open mailbox" "read leaflet" "drop leaflet"
"read news" "drop news" "put bird nest in case"
"move leaves" "open grating" "open egg"
"open window" "enter" "take painting"
"turn on lamp" "move rug" "open trap door"
"drop egg" "put painting in case" "open sack"
"jump")
# Calculate the size of zorkCommands
commandSize=${#zorkCommands[@]}
# Loop through and inset a random command at each step
for STEP in {1..500}; do
# Get the index of the zorkCommands
commandIndex=$(($RANDOM % $commandSize))
# Append into testInput
echo ${zorkCommands[$commandIndex]} >> testInput
# Run the game with the input and save the game text
./zork < testInput &> ZorkOut.gameOut
# Generate the coverage information
fd -d 1 -e gcda -X gcov > /dev/null
# Make directory to save the output
mkdir -p /home/COMS665A/ZorkData/CoverageAuto/Game$GAME/Step$STEP
# echo Moving to CoverageAuto/Game$GAME/Step$STEP
cp testInput /home/COMS665A/ZorkData/CoverageAuto/Game$GAME/Step$STEP/input
fd -d 1 -e gcda -e gcov -e gameOut -X mv {} /home/COMS665A/ZorkData/CoverageAuto/Game$GAME/Step$STEP
done
# Remove the temporary folder
rm -rf /home/COMS665A/Temporary/Zork$GAME
#! /usr/bin/sh
# Script to run the program, generate its coverage information
# and move it to a directory
# Make directory to save the output
GAME=${1?Error:Game number tag not provided}
# Start fresh
cp -r zork Temporary/Zork$GAME
cd Temporary/Zork$GAME
make -s
# Populate some commands from the game
zorkCommands=(
# Movements x 10
# General commands x 5
# Kill commands x 3
# Specialized commands x 1
"east" "west" "north" "south" "up" "down"
"inventory" "diagnose" "take all" "drop all"
"kill troll with sword" "kill thief with knife"
"east" "west" "north" "south" "up" "down"
"kill troll with sword" "kill thief with knife"
"inventory" "diagnose" "take all" "drop all"
"east" "west" "north" "south" "up" "down"
"open mailbox" "read leaflet" "drop leaflet"
"inventory" "diagnose" "take all" "drop all"
"east" "west" "north" "south" "up" "down"
"read news" "drop news" "put bird nest in case"
"inventory" "diagnose" "take all" "drop all"
"move leaves" "open grating" "open egg"
"east" "west" "north" "south" "up" "down"
"inventory" "diagnose" "take all" "drop all"
"east" "west" "north" "south" "up" "down"
"kill troll with sword" "kill thief with knife"
"east" "west" "north" "south" "up" "down"
"open window" "enter" "take painting"
"east" "west" "north" "south" "up" "down"
"turn on lamp" "move rug" "open trap door"
"east" "west" "north" "south" "up" "down"
"drop egg" "put painting in case" "open sack"
"east" "west" "north" "south" "up" "down"
"jump")
# Calculate the size of zorkCommands
commandSize=${#zorkCommands[@]}
# Loop through and inset a random command at each step
for STEP in {1..500}; do
# Get the index of the zorkCommands
commandIndex=$(($RANDOM % $commandSize))
# Append into testInput
echo ${zorkCommands[$commandIndex]} >> testInput
# Run the game with the input and save the game text
./zork < testInput &> ZorkOut.gameOut
# Generate the coverage information
fd -d 1 -e gcda -X gcov > /dev/null
# Make directory to save the output
mkdir -p /home/COMS665A/ZorkData/CoverageAuto/Game$GAME/Step$STEP
# echo Moving to CoverageAuto/Game$GAME/Step$STEP
cp testInput /home/COMS665A/ZorkData/CoverageAuto/Game$GAME/Step$STEP/input
fd -d 1 -e gcda -e gcov -e gameOut -X mv {} /home/COMS665A/ZorkData/CoverageAuto/Game$GAME/Step$STEP
done
# Remove the temporary folder
rm -rf /home/COMS665A/Temporary/Zork$GAME
south
up
enter
kill thief with knife
up
east
up
turn on lamp
up
move leaves
east
enter
east
up
south
read news
up
south
turn on lamp
north
up
move leaves
west
up
up
south
inventory
take painting
up
enter
turn on lamp
kill thief with knife
up
up
up
east
up
up
up
up
up
up
drop leaflet
open grating
drop egg
up
up
up
up
up
turn on lamp
up
south
south
move rug
south
up
move rug
up
turn on lamp
move rug
up
west
up
east
up
up
up
up
drop all
turn on lamp
up
up
south
turn on lamp
up
take all
move rug
east
west
take painting
west
down
kill thief with knife
up
inventory
up
west
move leaves
move rug
move rug
west
move rug
up
up
drop news
move rug
up
east
up
up
east
drop egg
drop leaflet
east
up
up
enter
turn on lamp
up
open trap door
south
up
up
up
up
up
up
up
west
south
up
south
up
take painting
take all
up
up
kill thief with knife
up
west
move rug
drop all
enter
take all
up
up
up
up
east
west
move rug
east
enter
up
up
south
move leaves
up
drop all
west
east
up
south
up
west
kill thief with knife
move leaves
move rug
turn on lamp
up
up
south
up
turn on lamp
east
up
south
inventory
up
kill thief with knife
up
west
south
move rug
up
up
up
drop leaflet
drop news
kill troll with sword
inventory
south
east
up
drop all
up
move leaves
move leaves
drop egg
west
up
west
take all
south
east
inventory
west
up
open grating
up
up
enter
east
west
west
south
move rug
move leaves
drop all
up
east
up
east
read leaflet
south
up
up
up
up
move rug
open mailbox
up
up
up
east
turn on lamp
up
east
up
up
up
up
south
east
enter
down
west
move rug
up
enter
drop all
inventory
up
move rug
up
take all
drop leaflet
drop leaflet
move rug
up
move rug
up
up
west
east
west
up
move rug
up
up
up
read news
up
east
up
up
up
up
up
south
move rug
up
enter
west
up
up
take all
turn on lamp
up
turn on lamp
up
north
up
enter
drop all
west
move rug
inventory
up
up
up
south
kill thief with knife
open mailbox
up
drop leaflet
up
up
east
up
up
west
up
up
move rug
south
take all
up
take all
up
up
turn on lamp
south
up
up
kill thief with knife
south
inventory
up
move rug
up
up
up
take all
up
up
move rug
up
inventory
up
kill thief with knife
take painting
move rug
up
up
up
up
move rug
move rug
up
up
move rug
east
up
up
up
up
drop leaflet
put bird nest in case
up
up
read news
move leaves
drop all
move rug
up
south
drop all
south
south
up
east
up
kill troll with sword
move leaves
take painting
turn on lamp
up
kill thief with knife
move rug
move rug
up
up
move rug
up
take all
up
west
up
move rug
east
drop leaflet
east
up
east
drop leaflet
move leaves
east
up
up
up
up
kill troll with sword
up
move rug
west
up
move rug
up
west
east
move leaves
move leaves
kill thief with knife
up
east
kill thief with knife
up
south
up
west
enter
turn on lamp
up
up
south
east
open window
move rug
move rug
east
open grating
up
up
east
take all
south
up
move rug
up
north
move rug
up
west
turn on lamp
east
move leaves
up
east
up
up
take all
up
up
west
up
south
up
up
turn on lamp
up
move leaves
up
move rug
south
up
kill thief with knife
drop all
turn on lamp
up
move leaves
up
up
up
up
read leaflet
take all
inventory
east
move leaves
enter
move rug
take painting
south
east
inventory
drop egg
enter
up
up
enter
up
move rug
up
up
south
east
move rug
up
up
open grating
west
up
up
open mailbox
open window
inventory
kill troll with sword
east
#! /usr/bin/sh
# Script to run getCoverageRandom.sh in parallel
mkdir -p Temporary
GAMES=${1?:Error:Give number of games to generate}
seq $GAMES | parallel --bar 'sh getCoverageRandom.sh {}'
rmdir Temporary
#! /usr/bin/sh
# Script to run getCoverageWeightedRandom.sh in parallel
mkdir -p Temporary
GAMES=${1?:Error:Give number of games to generate}
seq $GAMES | parallel --bar 'sh ./getCoverageWeightedRandom.sh {}'
rmdir Temporary
#! /usr/bin/sh
# Script to run the program, generate its coverage information
# and move it to a directory
# Make directory to save the output
rm -r ./GanCoverage/Game*
\ No newline at end of file
%% Cell type:code id: tags:
``` python
import subprocess
#subprocess.call(['C:\Users\User\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Windows PowerShell', './getCoverageGAN.sh'])
subprocess.run(['C:\Program Files\Git\git-bash.exe', './getCoverageGAN.sh'])
```
%% Output
CompletedProcess(args=['C:\\Program Files\\Git\\git-bash.exe', './getCoverageGAN.sh'], returncode=0)
%% Cell type:code id: tags:
``` python
subprocess.run(['C:\Program Files\Git\git-bash.exe', './remove_output.sh'])
```
%% Output
CompletedProcess(args=['C:\\Program Files\\Git\\git-bash.exe', './remove_output.sh'], returncode=0)
%% Cell type:code id: tags:
``` python
```
import subprocess
#subprocess.call(['C:\Users\User\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Windows PowerShell', './getCoverageGAN.sh'])
subprocess.run(['C:\Program Files\Git\git-bash.exe', './getCoverageGAN.sh'])
zork @ 95b1fd1f
Subproject commit 95b1fd1feead1d52ce079678070f37daaf66766d
import torch
from torchtext import data
from torchtext import datasets
import random
import time
import os
import copy
import csv
import numpy as np
import torch.nn.utils.rnn as rnn_utils
from torch.utils.data.sampler import SubsetRandomSampler
# for when samples are each in their own subfolder,
class zork_dataset(torch.utils.data.Dataset):
def __init__(self, folder_path, low_bound, high_bound):
self.path = folder_path
self.game_ns = []
self.step_ns = []
for i in range(low_bound,high_bound+1):
# self.game_ns.append(tmp_game)
self.step_ns.append(str(i))
#parse and store labels
full_path = os.path.join(folder_path, "Game"+str(i))
info_paths = []
info_paths.append(os.path.join(full_path, "rooms.c.gcov")) # CHANGE if i want more files
info_paths.append(os.path.join(full_path, "nrooms.c.gcov")) # CHANGE if i want more files
label = self.get_coverage(info_paths) # grab coverage information from files
coverage_score = 0.0
for i in label:
if i > 0.5:
coverage_score += 1.0
coverage_denom = max(1, len(label))
coverage_score = coverage_score / coverage_denom
#print(coverage_score)
output_validity = self.parse_output(os.path.join(full_path, "ZorkOut.gameOut"))
#print(output_validity)
full_label = []
full_label.append(1.0)
full_label.append(output_validity)
full_label.append(coverage_score)
label_path = os.path.join(full_path, "label.csv")
with open(label_path, 'w', newline='') as myfile:
wr = csv.writer(myfile)
wr.writerow(full_label)
self.len = len(self.step_ns)
def __getitem__(self, index):
item_path = os.path.join(self.path, "Game"+self.step_ns[index])
input_path = os.path.join(item_path, "input")
label_path = os.path.join(item_path, "label.csv")
x = np.loadtxt(input_path, delimiter='\n', dtype='str')
x = x.tolist()
y = np.loadtxt(label_path, delimiter=',', dtype=np.float32)
return x, y
def __len__(self):
return self.len
def get_coverage(self, filepaths):
label = []
for filepath in filepaths:
info_f = open(filepath, "r")
info_lines = info_f.readlines()
i = 0
for y in info_lines:
i += 1
temp_str = y.rstrip().split()[0][0:-1] #grab line count - ':'
if temp_str == '-':
continue
if temp_str == '#####':
label.append(0)
else:
label.append(int(temp_str.replace("*","")))
return label
def parse_output(self, filepath):
info_f = open(filepath, "r")
info_lines = info_f.readlines()
i = 0
total = 0
error_msgs = {">I can't see one here.", ">You can't go that way.", ">The window is closed.",
">I could't find anything.", ">You are empty handed.", ">You are in perfect health.",
">You cannot climb any higher."}
for y in info_lines:
ystr = y.rstrip()
if len(ystr) and ystr[0] == '>':
total += 1
if(not (ystr in error_msgs)):
i += 1
return i / total
print("Export complete.")
\ No newline at end of file
File added
File added
File added
import torch
from torchtext import data
from torchtext import datasets
import random
import time
import os
import copy
import csv
import numpy as np
import torch.nn.utils.rnn as rnn_utils
from torch.utils.data.sampler import SubsetRandomSampler
path = os.path.join("..", "Coverage") #PATH TO GAME FOLDERS, MODIFY
game_lower_index = 31
game_upper_index = 130
class zork_dataset_steps(torch.utils.data.Dataset):
def __init__(self, folder_path):
self.path = folder_path
ds_path = os.path.join(folder_path, "665_data.txt")
ds_f = open(ds_path, "r")
ds_lines = ds_f.readlines()
self.game_ns = []
self.step_ns = []
for x in ds_lines:
xsp = x.split()
#info for retrieval
tmp_game = xsp[0][4:]
#for i in range(int(xsp[1])+1,int(xsp[1])+1):
for i in range(500,501):
self.game_ns.append(tmp_game)
self.step_ns.append(str(i))
#parse and store labels
full_path = os.path.join(folder_path, xsp[0], "Step"+str(i))
info_paths = []
info_paths.append(os.path.join(full_path, "rooms.c.gcov")) # CHANGE if i want more files
info_paths.append(os.path.join(full_path, "nrooms.c.gcov")) # CHANGE if i want more files
label = self.get_coverage(info_paths) # grab coverage information from files
coverage_score = 0.0
for i in label:
if i > 0.5:
coverage_score += 1.0
coverage_denom = max(1, len(label))
coverage_score = coverage_score / coverage_denom
#print(coverage_score)
output_validity = self.parse_output(os.path.join(full_path, "ZorkOut.gameOut"))
#print(output_validity)
full_label = []
full_label.append(1.0)
full_label.append(output_validity)
full_label.append(coverage_score)
label_path = os.path.join(full_path, "label.csv")
with open(label_path, 'w', newline='') as myfile:
wr = csv.writer(myfile)
wr.writerow(full_label)
self.len = len(self.step_ns)
def __getitem__(self, index):
item_path = os.path.join(self.path, "Game"+self.game_ns[index], "Step"+self.step_ns[index])
input_path = os.path.join(item_path, "input")
label_path = os.path.join(item_path, "label.csv")
x = np.loadtxt(input_path, delimiter='\n', dtype='str')
x = x.tolist()
y = np.loadtxt(label_path, delimiter=',', dtype=np.float32)
return x, y
def __len__(self):
return self.len
def get_coverage(self, filepaths):
label = []
for filepath in filepaths:
info_f = open(filepath, "r")
info_lines = info_f.readlines()
i = 0
for y in info_lines:
i += 1
temp_str = y.rstrip().split()[0][0:-1] #grab line count - ':'
if temp_str == '-':
continue
if temp_str == '#####':
label.append(0)
else:
label.append(int(temp_str.replace("*","")))
return label
def parse_output(self, filepath):
info_f = open(filepath, "r")
info_lines = info_f.readlines()
i = 0
total = 0
error_msgs = {">I can't see one here.", ">You can't go that way.",
">The window is closed.", ">I could't find anything.",
">You are empty handed.", ">You are in perfect health.",
">You cannot climb any higher."}
for y in info_lines:
ystr = y.rstrip()
if len(ystr) and ystr[0] == '>':
total += 1
if(not (ystr in error_msgs)):
i += 1
return i / total
# for when samples are each in their own subfolder,
class zork_dataset(torch.utils.data.Dataset):
def __init__(self, folder_path, low_bound, high_bound, real=1.0):
self.path = folder_path
self.game_ns = []
self.step_ns = []
self.real = real
for i in range(low_bound,high_bound+1):
# self.game_ns.append(tmp_game)
self.step_ns.append(str(i))
#parse and store labels
full_path = os.path.join(folder_path, "Game"+str(i))
info_paths = []
info_paths.append(os.path.join(full_path, "rooms.c.gcov")) # CHANGE if i want more files
info_paths.append(os.path.join(full_path, "nrooms.c.gcov")) # CHANGE if i want more files
label = self.get_coverage(info_paths) # grab coverage information from files
coverage_score = 0.0
for i in label:
if i > 0.5:
coverage_score += 1.0
coverage_denom = max(1, len(label))
coverage_score = coverage_score / coverage_denom
#print(coverage_score)
output_validity = self.parse_output(os.path.join(full_path, "ZorkOut.gameOut"))
#print(output_validity)
full_label = []
full_label.append(real)
full_label.append(output_validity)
full_label.append(coverage_score)
label_path = os.path.join(full_path, "label.csv")
with open(label_path, 'w', newline='') as myfile:
wr = csv.writer(myfile)
wr.writerow(full_label)
self.len = len(self.step_ns)
def __getitem__(self, index):
item_path = os.path.join(self.path, "Game"+self.step_ns[index])
input_path = os.path.join(item_path, "input")
label_path = os.path.join(item_path, "label.csv")
x = np.loadtxt(input_path, delimiter='\n', dtype='str')
x = x.tolist()
y = np.loadtxt(label_path, delimiter=',', dtype=np.float32)
return x, y
def __len__(self):
return self.len
def get_coverage(self, filepaths):
label = []
for filepath in filepaths:
info_f = open(filepath, "r")
info_lines = info_f.readlines()
i = 0
for y in info_lines:
i += 1
temp_str = y.rstrip().split()[0][0:-1] #grab line count - ':'
if temp_str == '-':
continue
if temp_str == '#####':
label.append(0)
else:
label.append(int(temp_str.replace("*","")))
return label
def parse_output(self, filepath):
info_f = open(filepath, "r")
info_lines = info_f.readlines()
i = 0
total = 0
error_msgs = {">I can't see one here.", ">You can't go that way.", ">The window is closed.",
">I could't find anything.", ">You are empty handed.", ">You are in perfect health.",
">You cannot climb any higher."}
for y in info_lines:
ystr = y.rstrip()
if len(ystr) and ystr[0] == '>':
total += 1
if(not (ystr in error_msgs)):
i += 1
return i / total
def write_vocab_tofile(vocab_list):
with open('zork_vocab.txt', 'w') as vocab_file:
for word in vocab_list:
vocab_file.write('%s\n' % word)
def my_stoi(TEXT, x):
return TEXT.vocab.stoi[x]
def my_stoi2(vocab_list, x):
return vocab_list.index(x)
def load_vocab_file():
vocab_list = []
file1 = open('zork_vocab.txt', 'r')
Lines = file1.readlines()
for line in Lines:
vocab_list.append(line.strip())
return vocab_list
def save_zorkdata(path, prefix, inputs, lengths, labels):
full_prefix = os.path.join(path, prefix)
torch.save(inputs, full_prefix + '_inputs.pt')
torch.save(lengths, full_prefix + '_lengths.pt')
torch.save(labels, full_prefix + '_labels.pt')
def load_zorkdata(path, prefix):
full_prefix = os.path.join(path, prefix)
inputs = torch.load(full_prefix + '_inputs.pt')
lengths = torch.load(full_prefix + '_lengths.pt')
labels = torch.load(full_prefix + '_labels.pt')
return inputs, lengths, labels
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment