Пример #1
0
        public async Task TestQ3()
        {
            var travisUUID = Environment.GetEnvironmentVariable("travis_uuid");

            if (travisUUID == null)
            {
                travisUUID = "";
            }

            string responseBody = await client.GetStringAsync(baseUrl + "tests/run/3/" + travisUUID);

            List <TestCase> testCases = JsonConvert.DeserializeObject <List <TestCase> >(responseBody);

            List <Answer> answers = new List <Answer>();

            foreach (var test in testCases)
            {
                try
                {
                    Answer   answer            = new Answer();
                    Q3Object input             = JsonConvert.DeserializeObject <Q3Object>(test.input);
                    var      cancellationToken = new CancellationTokenSource();
                    cancellationToken.CancelAfter(1000);
                    await Task.Run(() => answer = getThirdAnswer(input, test), cancellationToken.Token);

                    answers.Add(answer);
                }
                catch (TaskCanceledException _)
                {
                    Console.WriteLine("A test in Question 3 has timed out. Tests must complete within one second.");
                    answers.Add(new Answer()
                    {
                        questionNumber = 3,
                        testNumber     = test.testNumber,
                        correct        = "TIMED_OUT",
                        speed          = -1
                    });
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.StackTrace);
                }
            }

            if (travisUUID.Length > 0)
            {
                string ans = JsonConvert.SerializeObject(answers);
                await client.PostAsync(baseUrl + "answer/contestant/" + travisUUID + "/3", new StringContent(ans));
            }

            Assert.IsTrue(answers.All(x => x.correct == "CORRECT"));
        }
Пример #2
0
        Answer getThirdAnswer(Q3Object input, TestCase test)
        {
            var timer = new Stopwatch();

            timer.Start();

            var answer = Question3.Answer(input.scores, input.alice);

            timer.Stop();

            var timeTaken = ((double)timer.ElapsedTicks / Stopwatch.Frequency) * 1000000000;

            return(new Answer()
            {
                questionNumber = 3,
                testNumber = test.testNumber,
                correct = answer == test.output ? "CORRECT" : "INCORRECT",
                speed = timeTaken
            });
        }