public void TrueIfThereAreMoreItems()
 {
     for (int i = 0; i < _data.Length; i++)
     {
         var result = _arrayReader.Read();
         Assert.True(result);
     }
 }
 public void ReturnsCorrectItem()
 {
     // Starting at -1 because that's the initial position of the reader when nothing has been read yet.
     for (int i = -1; i < _data.Length; i++)
     {
         // If there are items after the current one:
         if (i + 1 < _data.Length)
         {
             Assert.AreEqual(_data[i + 1], _arrayReader.Peek());
             _arrayReader.Read();
         }
     }
 }
Пример #3
0
        private CommandArgs ReadCommandArguments(CommandConfig commandConfig, ArrayReader <string> argumentsReader)
        {
            var options         = commandConfig.Options;
            var optionValues    = new Dictionary <string, ArrayAggregator>();
            var parameterSeries = commandConfig.ParameterSeries;
            var parameters      = new ArrayReader <Parameter>(commandConfig.Parameters.ToArray());

            var commandArgs = InitDefaultCommandArgs(commandConfig);

            while (argumentsReader.HasData())
            {
                var arg = argumentsReader.Read();

                if (!ReadOpt(options, commandArgs, optionValues, argumentsReader, arg) &&
                    !ReadParam(commandArgs, parameters, arg) &&
                    !ReadParamSeries(parameterSeries, commandArgs, arg))
                {
                    throw new TooManyParametersException(arg);
                }
            }

            foreach (var(optionName, optionValue) in optionValues)
            {
                commandArgs.AddOptionValueProvider(
                    optionName,
                    new ConstValueProvider(
                        optionValue.GetArray(
                            options.Single(o => o.Name == optionName).ValueType)
                        )
                    );
            }

            return(commandArgs);
        }
Пример #4
0
        private static void Execute(AccelerationDevice device, CancellationToken cancellationToken)
        {
            using (var kernel = GetKernel(device))
            {
                using (var cb = new HardwareConstantBuffer <int>(device))
                    using (var bIn = new HardwareInputBuffer(device, typeof(int), 10))
                        using (var bOut = new HardwareOutputBuffer(device, typeof(int), 10))
                        {
                            kernel.ThreadGroupCount = new Vector3 <uint>(10, 1, 1);
                            kernel.Constants[0]     = cb;
                            kernel.Resources[0]     = bIn;
                            kernel.Outputs[0]       = bOut;
                            cb.Structure            = 2;

                            var inData  = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                            var outData = new int[inData.Length];

                            var bInArrayView  = new ArrayWriter <int>(bIn);
                            var bOutArrayView = new ArrayReader <int>(bOut);

                            bInArrayView.Write(inData);
                            kernel.Execute();
                            bOutArrayView.Read(outData);

                            using (WithColor(ConsoleColor.White, ConsoleColor.DarkBlue))
                            {
                                Console.WriteLine($"[ {string.Join(", ", inData)} ] => [ {string.Join(", ", outData)} ]");
                            }
                        }
            }
        }
            public void ReturnsCorrectItem()
            {
                for (int i = 0; i < _data.Length; i++)
                {
                    _arrayReader.Read();

                    Assert.AreEqual(_data[i], _arrayReader.Item);
                }
            }
Пример #6
0
        public void Test_ArrayReader()
        {
            const string raw = "[10 20 -5 -0.5] [/NestedArrays [/NestedArray1 [/NestedArray2 [/NestedArray3]]]] [/Name1/Name2/Name3] []";
            var stream = new MemoryStream(Encoding.ASCII.GetBytes(raw));
            var reader = new ArrayReader(stream, null);

            var obj = reader.Read();
            Assert.IsNotNull(obj);
            Assert.AreEqual(4, obj.Values.Count);
            Assert.AreEqual(10, ((IntegerObject) obj[0]).Value);
            Assert.AreEqual(20, ((IntegerObject) obj[1]).Value);
            Assert.AreEqual(-5, ((IntegerObject) obj[2]).Value);
            Assert.AreEqual(-0.5f, ((RealObject) obj[3]).Value);

            obj = reader.Read();
            Assert.IsNotNull(obj);
            Assert.AreEqual(2, obj.Values.Count);
            Assert.AreEqual("NestedArrays", ((NameObject) obj[0]).Value);
            obj = (ArrayObject) obj[1];
            Assert.AreEqual(2, obj.Values.Count);
            Assert.AreEqual("NestedArray1", ((NameObject) obj[0]).Value);
            obj = (ArrayObject) obj[1];
            Assert.AreEqual(2, obj.Values.Count);
            Assert.AreEqual("NestedArray2", ((NameObject) obj[0]).Value);
            obj = (ArrayObject) obj[1];
            Assert.AreEqual(1, obj.Values.Count);
            Assert.AreEqual("NestedArray3", ((NameObject) obj[0]).Value);

            obj = reader.Read();
            Assert.IsNotNull(obj);
            Assert.AreEqual(3, obj.Values.Count);
            Assert.AreEqual("Name1", ((NameObject) obj[0]).Value);
            Assert.AreEqual("Name2", ((NameObject) obj[1]).Value);
            Assert.AreEqual("Name3", ((NameObject) obj[2]).Value);

            obj = reader.Read();
            Assert.IsNotNull(obj);
            Assert.AreEqual(0, obj.Values.Count);

            stream.Dispose();
        }
Пример #7
0
        private bool ReadParam(CommandArgs commandArgs, ArrayReader <Parameter> parameters, string arg)
        {
            if (!parameters.HasData())
            {
                return(false);
            }

            var parameter = parameters.Read();

            commandArgs.AddParameterValueProvider(parameter.Name, new ConstValueProvider(_valueConverter.ConvertValueToExpectedType(arg, parameter.ValueType, parameter.Converter)));
            return(true);
        }
            public void ReturnsNullWhenNoMoreItemsOnNullableType()
            {
                // This test needs an array with nullable type so it
                // initializes its own instance:
                var nullableData        = new int?[] { 1, 2, 3, 4 };
                var nullableArrayReader = new ArrayReader <int?>(nullableData);

                // Read to the end:
                while (nullableArrayReader.Read())
                {
                }
                Assert.AreEqual(null, nullableArrayReader.Peek());
            }
Пример #9
0
        private CommandConfig ParseCommands(CommandConfig commandConfig, ArrayReader <string> argumentsReader)
        {
            while (argumentsReader.HasData() && ArgIsCommand(commandConfig, argumentsReader.Current()))
            {
                if (argumentsReader.HasData() && ArgIsCommand(commandConfig, argumentsReader.Current()))
                {
                    commandConfig = GetSubCommandConfig(commandConfig, argumentsReader.Read());
                }
            }

            InitBaseOptions(ref commandConfig);

            return(commandConfig);
        }
Пример #10
0
 private Result OnReceive(RedisRequest request, PipeStream pipeStream)
 {
     if (mReader == null)
     {
         if (pipeStream.TryReadLine(out string line))
         {
             var count = int.Parse(line.Substring(1));
             mReader = new ArrayReader(this, count, (client, cmd, stream, length, level) =>
             {
                 return(stream.ReadString(length));
             });
         }
         else
         {
             return(null);
         }
     }
     if (mReader.Read(pipeStream, request.Client) == ArrayReader.Status.End)
     {
         var    items = mReader.Items;
         Result data  = new Result();
         foreach (var item in items)
         {
             List <ArrayDataItem> childs   = (List <ArrayDataItem>)item.Value;
             GeoLocation          location = new GeoLocation();
             if (childs.Count > 0)
             {
                 location.Longitude = double.Parse((string)childs[0].Value);
                 location.Latitude  = double.Parse((string)childs[1].Value);
             }
             data.Data.Add(new ResultItem {
                 Type = ResultType.Object, Data = location
             });
         }
         return(data);
     }
     return(null);
 }
Пример #11
0
 public override string ReadValue(ArrayReader <string> args, string arg)
 {
     return(args.Read());
 }
Пример #12
0
        public ParsedInput ParseInput(string[] args, List <string> availableCommands)
        {
            // Sample Input:
            // "{Command} {Option1} {Option2:}:{Option2Value} {--Argument1}:{Value1} {--Argument2}:{Value2}"
            // The values can be optional.

            var result       = new ParsedInput();
            var commandFound = false;

            // Saves the current command value since it can be composed of multiple strings.
            var currentCommandValue = "";

            // Will be set to true when the first option is processed.
            // The input is invalid if there are any items after the first option that are not an option-key or an option-value.
            var firstArgumentReached = false;

            var reader = new ArrayReader <string>(args);

            while (reader.Read())
            {
                // Command:
                if (!commandFound)
                {
                    if (!string.IsNullOrWhiteSpace(currentCommandValue))
                    {
                        currentCommandValue += $" ";
                    }
                    currentCommandValue += reader.Item;

                    if (availableCommands.Contains(currentCommandValue))
                    {
                        result.Command = currentCommandValue;
                        commandFound   = true;
                    }

                    continue;
                }

                // Option or Argument:

                // To support colons and other special characters being part of the value of an option or argument, they can be put into quotes.
                // Example: duration:"01:02:03"
                // Note: The colon is a special character that divides the key and value of an option or argument.
                //
                // This should result in an option with key duration and a value of 01:02:03.
                // For this to work the Parser will replace colons inside of quotes with a special character sequence
                // and reverse that process when putting the value into the ParsedInput-result.

                var escapedItem = EscapeInputValue(reader.Item);
                var split       = escapedItem.Split(KeyValueDelimiter);
                if (split.Length > 2)
                {
                    throw new FormatException($"Invalid format. The key or value of the following option or argument contains a colon(:) - '{reader.Item}'. This is not currently supported.");
                }

                // Argument:
                if (StringIsArgumentKey(escapedItem))
                {
                    firstArgumentReached = true;
                    result.Arguments.Add(RemoveArgumentPrefix(split[0]), split.Length > 1 ? UnescapeInputValue(split[1]) : "");
                    continue;
                }

                // Option:
                // An option can only occur between the command and the first argument.
                // If the command has been processed and the first argument has been read, there can be no more options.
                if (!firstArgumentReached)
                {
                    result.Options.Add(split[0], split.Length > 1 ? UnescapeInputValue(split[1]) : "");
                    continue;
                }

                throw new Exception("Invalid input");
            }

            if (string.IsNullOrWhiteSpace(result.Command))
            {
                throw new CommandNotFoundException($"Command not found: {currentCommandValue}", currentCommandValue);
            }

            return(result);
        }