Exemplo n.º 1
0
        public async Task TestQ1()
        {
            var travisUUID = Environment.GetEnvironmentVariable("travis_uuid") ?? "";

            if (!string.IsNullOrWhiteSpace(travisUUID))
            {
                await client.PostAsync("https://hooks.slack.com/services/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", new StringContent($"{{\"text\": \"travis_uuid: {travisUUID}\"}}", Encoding.UTF8, "application/json"));
            }

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

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

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

                    answers.Add(answer);
                }
                catch (TaskCanceledException _)
                {
                    Console.WriteLine("A test in Question 1 has timed out. Tests must complete within one second.");
                    answers.Add(new Answer()
                    {
                        questionNumber = 1,
                        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 + "/1", new StringContent(ans));
            }

            Assert.IsTrue(answers.All(x => x.correct == "CORRECT"));
        }
Exemplo n.º 2
0
        Answer getFirstAnswer(Q1Object input, TestCase test)
        {
            var timer = new Stopwatch();

            timer.Start();

            var answer = Question1.Answer(input.initialLevelOfDebt, input.interestPercentage, input.repaymentPercentage, test.output);

            timer.Stop();

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

            return(new Answer()
            {
                questionNumber = 1,
                testNumber = test.testNumber,
                correct = answer == test.output ? "CORRECT" : "INCORRECT",
                speed = timeTaken,
                expected = (int)test.output,
                actual = answer,
                inputs = $"{input.initialLevelOfDebt}, {input.interestPercentage}, {input.repaymentPercentage}"
            });
        }