public async Task TestQ4() { var travisUUID = Environment.GetEnvironmentVariable("travis_uuid"); if (travisUUID == null) { travisUUID = ""; } string responseBody = await client.GetStringAsync(baseUrl + "tests/run/4/" + 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(); Q4Object input = JsonConvert.DeserializeObject <Q4Object>(test.input); var cancellationToken = new CancellationTokenSource(); cancellationToken.CancelAfter(1000); await Task.Run(() => answer = getFourthAnswer(input, test), cancellationToken.Token); answers.Add(answer); } catch (TaskCanceledException _) { Console.WriteLine("A test in Question 4 has timed out. Tests must complete within one second."); answers.Add(new Answer() { questionNumber = 4, 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 + "/4", new StringContent(ans)); } Assert.IsTrue(answers.All(x => x.correct == "CORRECT")); }
Answer getFourthAnswer(Q4Object input, TestCase test) { var timer = new Stopwatch(); timer.Start(); var answer = Question4.Answer(input.v, input.c, input.mc); timer.Stop(); var timeTaken = ((double)timer.ElapsedTicks / Stopwatch.Frequency) * 1000000000; return(new Answer() { questionNumber = 4, testNumber = test.testNumber, correct = answer == test.output ? "CORRECT" : "INCORRECT", speed = timeTaken }); }