코드를 접어두니 블로그 글이 훨씬 짧아졌습니다.
예시 코드
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
int case_count;
int main(){
while(true){
Sleep(50);
system("generator.exe"); // 데이터 생성
system("solution1.exe"); // 첫 번째 솔루션 실행
system("solution2.exe"); // 두 번째 솔루션 실행
printf("-----------Test Case %d----------\n", ++case_count);
if(system("fc output1.txt output2.txt")) system("pause");
}
return 0;
}
freopen을 사용하지 않는 버전
#include <cstdio>
#include <cstdlib>
#include <windows.h>
using namespace std;
int main()
{
int test_num = 0;
while(true)
{
printf("Test Data %d :\n", ++test_num);
system("gen.exe > input.txt"); // 입력 데이터 생성
system("program_a.exe < input.txt > out_a.txt"); // 프로그램 A 출력
system("program_b.exe < input.txt > out_b.txt"); // 프로그램 B 출력
if(system("fc out_a.txt out_b.txt"))
{
system("pause"); // 차이점 확인용 일시정지
// input.txt에 문제가 저장되어 있으므로 디버깅 가능
}
}
}
주석 없는 간결 버전
#include <cstdio>
#include <cstdlib>
#include <windows.h>
using namespace std;
int main()
{
int count = 0;
while(true)
{
printf("Running Test %d:\n", ++count);
system("data_maker.exe > input.dat");
system("solver_v1.exe < input.dat > result1.txt");
system("solver_v2.exe < input.dat > result2.txt");
if(system("fc result1.txt result2.txt"))
system("pause");
}
}
데이터 생성기 예제
// 난수 기반 테스트 케이스 생성기
#include <ctime>
#include <cstdio>
#include <cstdlib>
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
typedef long long ll;
const int N = 100005;
int n;
ll random_val(ll min_val, ll max_val) {
return ((ll)rand() * RAND_MAX + rand()) % (max_val - min_val + 1) + min_val;
}
void write_number(ll num) {
if(num < 0) { putchar('-'); num = -num; }
if(num > 9) write_number(num / 10);
putchar('0' + num % 10);
}
int main()
{
// freopen("input.in","w",stdout);
srand((unsigned)time(NULL));
n = random_val(1, 1000);
write_number(n); putchar('\n');
rep(i,1,n) {
write_number(random_val(-10000, 10000));
putchar(' ');
}
return 0;
}
주의: 파일 이름은 확장자가 다르더라도 동일한 이름을 사용하면 안 됩니다.