Example #1
0
        public async Task <IActionResult> UploadFile(IFormFile file)
        {
            if (file != null)
            {
                InputConvertingData inputData;
                using (var fileStream = new StreamReader(file.OpenReadStream(), Encoding.UTF8))
                {
                    inputData = JsonConvert.DeserializeObject <InputConvertingData>(await fileStream.ReadToEndAsync());
                }

                var requests = new List <RequestModel>();
                foreach (var text in inputData.Data)
                {
                    Permutation.PermutationText = text;
                    Stopwatch sw = Stopwatch.StartNew();

                    if (CachedData.ContainsKey(text))
                    {
                        CachedData[text].OutputData.Time = sw.Elapsed;
                        requests.Add(CachedData[text]);
                    }
                    else
                    {
                        RequestModel model = new RequestModel
                        {
                            InputData  = text,
                            OutputData = new ResponseModel
                            {
                                OutList = Permutation.GetSortedPermutationsList(),
                                Time    = sw.Elapsed
                            }
                        };
                        requests.Add(model);
                        CachedData.Add(text, model);
                    }

                    sw.Stop();
                }

                var    path          = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "JsonResult.json");
                string serializeData = JsonConvert.SerializeObject(requests);
                using (var stream = new FileStream(path, FileMode.Create))
                {
                    await stream.WriteAsync(Encoding.UTF8.GetBytes(serializeData));
                }
                return(View("Permutations", requests));
            }

            return(View("Index"));
        }
Example #2
0
        public void GetSortedPermutationsListTest(string text, string[] textResults)
        {
            Permutations p = new Permutations(text);

            Assert.Equal(text, p.PermutationText);
            Assert.Equal(1, p.CountItems);
            p.GetSortedPermutationsList();
            Assert.Equal(textResults.Length, p.CountItems);
            for (var index = 0; index < textResults.Length; index++)
            {
                var t = textResults[index];
                Assert.Equal(t, p.GetList()[index]);
            }
        }