powershell批處理io校驗(yàn)功能
powershell批處理——io校驗(yàn)
在刷題時,時?;叵?,OJ平臺是如何校驗(yàn)競賽隊員提交的代碼的,OJ平臺并不看代碼,而是使用“黑盒測試”,用測試數(shù)據(jù)來驗(yàn)證。對于每題,都事先設(shè)定了很多組輸入數(shù)據(jù)(datain)和輸出數(shù)據(jù)(dataout),OJ主要是看輸入datain,看產(chǎn)生的輸出是否與dataout一致。
如果需要一次性知道自己的所有測試輸入數(shù)據(jù)是否能得到正確輸出,一個一個測試就太慢了,批處理操作就能很好解決這個問題。剛好在學(xué)習(xí)powershell,本篇文章主要介紹一下這個ioCode項目。
項目目錄結(jié)構(gòu)

- input目錄:存放輸入數(shù)據(jù)文件,以
.in后綴結(jié)尾 - output目錄:存放輸出數(shù)據(jù)文件,以
.out后綴結(jié)尾,每個輸出的文件名與對應(yīng)的輸入的文件名相同 - start.ps1:編寫的腳本文件
start.ps1源代碼:
function ioCheck{
param(
$content, #程序輸出內(nèi)容
$answer #輸出的答案內(nèi)容
)
$cnt = $answer.length
if($content.length -ne $cnt){
return -1 #行數(shù)不一致返回-1
}
for($i=0;$i -lt $cnt;$i++){
if($answer[$i] -ne $content[$i]){
return $i+1 #內(nèi)容不一致,返回第一個不同的行數(shù)
}
}
return 0 #相同返回0
}
function ioCode{
param(
[string]$command,
[string]$inPath = './input',
[string]$outPath = './output'
)
$allStartTime = Get-Date
$acList = New-Object System.Collections.ArrayList #創(chuàng)建正確輸出的文件名集合
$errList = New-Object System.Collections.ArrayList #創(chuàng)建錯誤輸出的文件名集合
$inFiles = Get-ChildItem -Path $inPath -Filter *.in #獲取輸入文件集
$len = $inFiles.length
$cnt = 0
foreach($file in $inFiles){
try {
$startTime = Get-Date
$fullOut = (cmd.exe /c "$command < $($file.fullName)") -split "\r\n|\r|\n" #獲取程序?qū)嶋H輸出內(nèi)容
$endTime = Get-Date
$duration = $endTime - $startTime #獲取程序執(zhí)行時間
$answerPath = Join-Path -Path $outPath -ChildPath ($file.BaseName + '.out')
$answer = Get-Content -Path $answerPath -ReadCount 0 #獲取該輸入文件的對應(yīng)輸出文件的內(nèi)容,-ReadCount 0指定返回一個字符串?dāng)?shù)組
$res = ioCheck $fullOut $answer
if($res -eq 0){
$item = @{
fileName = $file.Name
exeTime = $duration
}
$acList.add($item) | Out-Null #Out-Null表示丟棄輸出
$cnt++
}else{
$item = @{
fileName = $file.Name
errLine = $res
}
$errList.add($item) | Out-Null
}
}
catch {
Write-Host "處理文件 $($file.Name) 時出現(xiàn)錯誤: $_"
}
}
$allEndTime = Get-Date
$allExeTime = $allEndTime-$allStartTime
Write-Host "正確輸出 $cnt/$len:,總耗時:$($allExeTime.Milliseconds)ms"
foreach($item in $acList){
Write-Host "Y: $($item.fileName) 耗時:$($item.exeTime.Milliseconds)ms"
}
foreach($item in $errList){
Write-Host "N: $($item.fileName) errCode:$($item.errLine)"
}
return @{
CorrectCount = $cnt
TotalCount = $len
acList = $acList
errList = $errList
}
}首先測試輸入兩個數(shù),看能否成功輸出兩數(shù)之和
在input目錄中創(chuàng)建三個文件分別為001.in,002.in,003.in
內(nèi)容分別為
3 4
35749 47985
544 6755
對應(yīng)的在output目錄創(chuàng)建三個同名的.out文件,分別為001.out,002.out,003.out
內(nèi)容就是對應(yīng).in文件中兩個輸入數(shù)據(jù)的相加的結(jié)果。
使用
編寫好測試用例,就是可以測試我們寫好的代碼了,這里不僅可以測試C/C++生成的.exe可執(zhí)行文件,還可以測試java生成的.class可執(zhí)行字節(jié)碼文件(前提是電腦具備java環(huán)境)。
測試.exe文件
在當(dāng)前目錄下準(zhǔn)備一個test.cpp文件,代碼如下:
#include<iostream>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
cout<< 7;
return 0;
}將其編譯為可執(zhí)行文件test.exe:
g++ -o test.exe test.cpp
在當(dāng)前目錄下打開powershell控制臺
通過點(diǎn)加載start.ps1文件
. './start.ps1'
輸入命令ioCode {command} {inPath} {outPath},command為可執(zhí)行命令,inPath為輸入文件目錄(默認(rèn)為當(dāng)前目錄的input目錄),outPath為輸出答案文件目錄(默認(rèn)為當(dāng)前目錄的output目錄)
ioCode test.exe
示例:

控制臺輸出顯示我們的三個測試用例都通過了。
測試可執(zhí)行.class文件
在當(dāng)前目錄下準(zhǔn)備一個Main.java文件,代碼如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
System.out.println(a+b);
}
}將其編譯為可執(zhí)行文件Main.class:
javac Main.java
在當(dāng)前目錄下打開powershell控制臺
通過點(diǎn)加載start.ps1文件
. './start.ps1'
輸入命令ioCode {command} {inPath} {outPath},command為可執(zhí)行命令,inPath為輸入文件目錄(默認(rèn)為當(dāng)前目錄的input目錄),outPath為輸出答案文件目錄(默認(rèn)為當(dāng)前目錄的output目錄)
ioCode 'java Main' #注意java Main是一個完整的命令,中間帶有空格,需要用引號引起來
示例:

測試可執(zhí)行.py文件
在當(dāng)前目錄下準(zhǔn)備一個Main.java文件,代碼如下:
a,b=map(int,input().split()) print(a+b)
在當(dāng)前目錄下打開powershell控制臺
通過點(diǎn)加載start.ps1文件
. './start.ps1'
輸入命令ioCode {command} {inPath} {outPath},command為可執(zhí)行命令,inPath為輸入文件目錄(默認(rèn)為當(dāng)前目錄的input目錄),outPath為輸出答案文件目錄(默認(rèn)為當(dāng)前目錄的output目錄)
ioCode 'python test.py' #注意python test.py是一個完整的命令,中間帶有空格,需要用引號引起來
示例:

以上就是這個小項目的內(nèi)容,通過ioCode可以批處理測試多個輸入輸出用例,并能清晰的看到哪些輸入文件正確輸出方便DeBug。
到此這篇關(guān)于powershell批處理——io校驗(yàn)的文章就介紹到這了,更多相關(guān)powershell批處理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Powershell獲取系統(tǒng)中所有可停止的服務(wù)
這篇文章主要介紹了Powershell獲取系統(tǒng)中所有可停止的服務(wù),本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-03-03
Windows Powershell 執(zhí)行外部命令
Windows PowerShell 在使用方面與 Cmd.exe 并無多大不同,只是 Windows PowerShell 的功能更為強(qiáng)大。與 Cmd.exe 一樣,Windows PowerShell 具有內(nèi)置的腳本編寫語言,不過它比 Cmd.exe 原始的批處理語言更為靈活。Cmd.exe 做到的事情,Windows PowerShell 幾乎都能做到。2014-08-08
PowerShell小技巧之獲取Windows系統(tǒng)密碼Hash
這篇文章主要介紹了使用PowerShell獲取Windows系統(tǒng)密碼Hash的小技巧,非常的實(shí)用,需要的朋友可以參考下2014-10-10
windows Powershell 快速編輯模式和標(biāo)準(zhǔn)模式
powershell控制臺有兩種模式,一個是快速編輯模式,一個是標(biāo)準(zhǔn)模式。2014-08-08
PowerShell入門教程之快速學(xué)習(xí)PowerShell的幾個方法
這篇文章主要介紹了PowerShell入門教程之快速學(xué)習(xí)PowerShell的幾個方法,本文提出了3種快速學(xué)習(xí)PowerShell的方法,需要的朋友可以參考下2014-10-10

