XOR Circuit用のテスターを書いてみた.

デバッグが非常に大変だったので.

Imports System
Imports System.Text
Imports System.IO
Imports System.Diagnostics
Module Module1
    Public output As New StringBuilder
    Public sw As StreamWriter
    Public n As Integer, k As Integer

    Public res() As Integer
    Public qc As Integer = 0
    Public rnds As New Random
    Public p As Process
    Sub Make_TestCase()
        n = 10000
        k = 10
        res = New Integer(k - 1) {}
        For i = 0 To k - 1
            res(i) = rnds.Next(n) 'きっと重複しないと思う
        Next
        Array.Sort(res)
    End Sub
    Sub Main()
        p = New Process()


        p.StartInfo.FileName = "2277.exe"
        p.StartInfo.CreateNoWindow = True
        p.StartInfo.UseShellExecute = False
        p.StartInfo.RedirectStandardOutput = True
        p.StartInfo.RedirectStandardInput = True

        AddHandler p.OutputDataReceived, AddressOf OutputHandler
        Make_TestCase()

        p.Start()

        p.BeginOutputReadLine()
        sw = p.StandardInput

        sw.WriteLine("{0} {1}", n, k)

        p.WaitForExit()
        sw.Dispose()
        p.Dispose()


    End Sub

    Public Sub OutputHandler(ByVal e As Object, ByVal args As DataReceivedEventArgs)
        Dim indata As String = args.Data
        If indata Is Nothing Then
            Return
        End If
        If indata(0) = "!"c Then
            indata = indata.Substring(1)
            Dim result As String() = indata.Split(" ")
            For i = 0 To result.Length - 1
                If Integer.Parse(result(i)) - 1 <> res(i) Then
                    Console.WriteLine()
                    Console.WriteLine("------")
                    Console.WriteLine("WrongAnswer")
                    Console.Write("Your Answer:")
                    For j = 0 To result.Length - 1
                        Console.Write("{0} ", result(j))
                    Next
                    Console.WriteLine()
                    Console.Write("Correct Answer:")
                    For j = 0 To res.Length - 1
                        Console.Write("{0} ", res(j) + 1)
                    Next

                    Console.WriteLine()
                    Console.WriteLine("Query Count: {0}", qc)
                    Exit Sub
                End If
            Next
            Console.WriteLine()
            Console.WriteLine("------")
            Console.WriteLine("Accepted!!")
            Console.Write("Your Answer:")
            For j = 0 To result.Length - 1
                Console.Write("{0} ", result(j))
            Next
            Console.WriteLine()
            Console.Write("Correct Answer:")
            For j = 0 To res.Length - 1
                Console.Write("{0} ", res(j) + 1)
            Next

            Console.WriteLine()
            Console.WriteLine("Query Count: {0}", qc)
        ElseIf indata(0) = "?"c Then
            indata = indata.Substring(1)
            Console.WriteLine("Program Query:{0}", indata)
            If indata.Length < n Then
                Console.WriteLine("Error!!")
                Exit Sub
            End If
            Dim ans As Integer = 0
            For i = 0 To k - 1
                If indata(res(i)) = "1"c Then
                    ans += 1
                End If
            Next
            ans = ans Mod 2
            Console.WriteLine("Query Result:{0}", ans)
            sw.WriteLine(ans)
            qc += 1
            If qc > 200 Then
                Console.WriteLine()
                Console.WriteLine("------")
                Console.WriteLine("Query Limit Excceed!!")
                p.Kill()
            End If
        Else
            Console.WriteLine("Program Output:{0}", indata)
        End If
    End Sub
End Module