< Summary

Information
Class: DeltaSharp.TargetGeneratorOperation
Assembly: DeltaSharp
File(s): /home/runner/work/DeltaSharp/DeltaSharp/src/DeltaSharp/TestHelper/TargetGenerator.cs
Line coverage
76%
Covered lines: 105
Uncovered lines: 32
Coverable lines: 137
Total lines: 332
Line coverage: 76.6%
Branch coverage
70%
Covered branches: 63
Total branches: 89
Branch coverage: 70.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Op()100%11100%
get_Args()100%11100%
.ctor()100%11100%
Apply(...)80%55100%
ApplyAdd(...)79.16%362472.72%
ApplySet(...)64.28%592865.85%
ApplyDel(...)62.5%10870%
ApplySwap(...)62.5%211672.22%
GetHashed(...)100%22100%
ParseOperation(...)50%2288.88%
Parse(...)100%44100%

File(s)

/home/runner/work/DeltaSharp/DeltaSharp/src/DeltaSharp/TestHelper/TargetGenerator.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.IO;
 4using System.Linq;
 5using System.Text;
 6using System.Threading.Tasks;
 7
 8namespace DeltaSharp;
 9
 10
 11internal class TargetGenerator
 12{
 13    public byte[] Source => Encoding.UTF8.GetBytes(string.Join("\n", SourceLines) + "\n");
 14
 15    public List<string> SourceLines { get; private set; }
 16
 17    Random _random;
 18
 19    public TargetGenerator(string? source = null, Random? random=null)
 20    {
 21        _random = random ?? new Random();
 22
 23        if (source is not null)
 24        {
 25            var lines = source.Split('\n').Select(l => l.TrimEnd('\r')).ToList();
 26            SourceLines = lines;
 27        }
 28        else
 29        {
 30            SourceLines = new List<string>();
 31        }
 32    }
 33
 34    public TargetGenerator(IEnumerable<string> lines, Random? random = null)
 35    {
 36        _random = random ?? new Random();
 37
 38        SourceLines = lines.ToList();
 39    }
 40
 41    public void Execute(string input)
 42    {
 43        var lines = input.Split('\n').Select(l => l.TrimEnd('\r')).ToArray();
 44
 45        lines = lines.SelectMany(l => l.Split(';')).Select(l => l.Trim()).ToArray();
 46
 47        var target = new List<string>(SourceLines);
 48
 49        var operations = TargetGeneratorOperation.Parse(lines);
 50
 51        Execute(operations);
 52
 53    }
 54
 55    public void Execute(TargetGeneratorOperation[] operations)
 56    {
 57        var target = SourceLines;
 58
 59        foreach (var op in operations)
 60            op.Apply(target,_random);
 61
 62    }
 63
 64}
 65
 66internal enum OperationEnum
 67{
 68    Set,
 69    Add,
 70    Del,
 71    Swp
 72}
 73
 74internal class TargetGeneratorOperation
 75{
 48076    public OperationEnum Op { get; set; }
 77
 185078    public string[] Args { get; set; } = Array.Empty<string>();
 79
 80
 24081    private TargetGeneratorOperation()
 82    {
 24083    }
 84
 85    public void Apply(List<string> source,Random random)
 86    {
 24087        switch (Op)
 88        {
 6089            case OperationEnum.Add: ApplyAdd(source, random); break;
 6090            case OperationEnum.Del: ApplyDel(source); break;
 30091            case OperationEnum.Set: ApplySet(source, random); break;
 3092            case OperationEnum.Swp: ApplySwap(source); break;
 93        }
 3094    }
 95
 96    private void ApplyAdd(List<string> source,Random random)
 97    {
 3098        if (Args.Length <1)
 099            throw new InvalidDataException("[ApplyAdd] invalid argument count");
 100
 30101        if (!int.TryParse(Args[0],out var position))
 0102            throw new InvalidDataException("[ApplyAdd] invalid position");
 103
 30104        if (position > source.Count - 1 || position == -1)
 30105            position = source.Count;
 106
 30107        var buffer = new byte[16];
 108
 30109        var count = 1;
 30110        var data = string.Empty;
 30111        if (Args.Length > 1)
 112        {
 30113            data = Args[1];
 114
 30115            if (Args.Length > 2)
 116            {
 20117                if (!int.TryParse(Args[2], out count))
 0118                    throw new InvalidDataException("[ApplySet] invalid count");
 119            }
 120        }
 121        else
 122        {
 0123            random.NextBytes(buffer);
 124
 0125            data = Convert.ToBase64String(buffer, Base64FormattingOptions.None)
 0126                    .Replace("==", "").Replace("=", "");
 127        }
 128
 129
 200130        while (count > 0)
 131        {
 170132            var s = data;
 170133            if (data == "RND")
 134            {
 0135                random.NextBytes(buffer);
 136
 0137                s = Convert.ToBase64String(buffer, Base64FormattingOptions.None)
 0138                    .Replace("==", "").Replace("=", "");
 139
 140            }
 170141            else if (data.StartsWith("RND(") && data.EndsWith(")"))
 142            {
 160143                var numberString = data.Replace("RND(", "").Replace(")", "");
 160144                var randCount = int.Parse(numberString);
 145
 160146                var tmp = new byte[randCount];
 5120147                for (var i = 0; i < tmp.Length; i++)
 2400148                    tmp[i] = (byte)random.Next(32, 127);
 149
 160150                s = Encoding.UTF8.GetString(tmp).Replace('\r', 'r').Replace('\n', 'n');
 151            }
 152
 170153            source.Insert(position, s);
 170154            count--;
 155        }
 156
 30157    }
 158
 159    private void ApplySet(List<string> source,Random random)
 160    {
 150161        if (Args.Length < 1)
 0162            throw new InvalidDataException("[ApplySet] invalid argument count");
 163
 150164        if (!int.TryParse(Args[0], out var position))
 0165            throw new InvalidDataException("[ApplySet] invalid position");
 166
 150167        if(position>source.Count-1)
 0168            throw new InvalidDataException("[ApplySet] invalid position");
 169
 150170        var buffer = new byte[16];
 171
 150172        int count = 1;
 150173        int step = 1;
 150174        var data = string.Empty;
 175
 150176        if (Args.Length > 1)
 177        {
 150178            data = Args[1];
 179
 150180            if (Args.Length > 2)
 181            {
 20182                if (!int.TryParse(Args[2], out count))
 0183                    throw new InvalidDataException("[ApplySet] invalid count");
 184            }
 185
 150186            if (Args.Length > 3)
 187            {
 20188                if (!int.TryParse(Args[3], out step))
 0189                    throw new InvalidDataException("[ApplySet] invalid step");
 190            }
 191        }
 192        else
 193        {
 194
 0195            random.NextBytes(buffer);
 196
 0197            data = Convert.ToBase64String(buffer, Base64FormattingOptions.None)
 0198                    .Replace("==", "").Replace("=", "");
 199        }
 200
 340201        while(count > 0)
 202        {
 190203            var i = position + (count - 1) * step;
 204
 190205            var s = data;
 190206            if (data == "RND")
 207            {
 60208                random.NextBytes(buffer);
 60209                s = Convert.ToBase64String(buffer, Base64FormattingOptions.None)
 60210                   .Replace("==", "").Replace("=", "");
 211            }
 130212            else if (data.StartsWith("RND(") && data.EndsWith(")"))
 213            {
 0214                var numberString = data.Replace("RND(", "").Replace(")", "");
 0215                var randCount = int.Parse(numberString);
 216
 0217                var tmp = new byte[randCount];
 0218                for (var z = 0; z < tmp.Length; z++)
 0219                    tmp[z] = (byte)random.Next(32, 127);
 220
 0221                s = Encoding.UTF8.GetString(tmp).Replace('\r', 'r').Replace('\n', 'n');
 222            }
 130223            else if (data == "HASH")
 224            {
 120225                var current = source[i];
 120226                s = GetHashed(current);
 227            }
 228
 190229            source[i] = s;
 230
 190231            count--;
 232        }
 150233    }
 234
 235    private void ApplyDel(List<string> source)
 236    {
 30237        if (Args.Length < 1)
 0238            throw new InvalidDataException("[ApplyDel] invalid argument count");
 239
 30240        if (!int.TryParse(Args[0], out var position))
 0241            throw new InvalidDataException("[ApplyDel] invalid position");
 242
 30243        var count = 1;
 30244        if (Args.Length > 1)
 245        {
 30246            if (!int.TryParse(Args[1], out count))
 0247                throw new InvalidDataException("[ApplyDel] invalid count");
 248        }
 249
 30250        source.RemoveRange(position, count);
 30251    }
 252
 253    private void ApplySwap(List<string> source)
 254    {
 30255        if (Args.Length < 2)
 0256            throw new InvalidDataException("[ApplySwap] invalid argument count");
 257
 30258        if (!int.TryParse(Args[0], out var n1))
 0259            throw new InvalidDataException("[ApplySwap] invalid position 1");
 260
 30261        if (!int.TryParse(Args[1], out var n2))
 0262            throw new InvalidDataException("[ApplySwap] invalid position 2");
 263
 30264        int n3 = 1;
 30265        if(Args.Length > 2)
 266        {
 267            //swap block
 268
 20269            if (!int.TryParse(Args[2], out n3))
 0270                throw new InvalidDataException("[ApplySwap] invalid position 2");
 271
 272        }
 30273        if (n2 + n3 > source.Count || n1 + n3 > source.Count)
 0274            throw new InvalidDataException("[ApplySwap] invalid range");
 275
 240276        foreach (var i in Enumerable.Range(0, n3))
 277        {
 90278            var s = source[n1 + i];
 90279            var s2 = source[n2 + i];
 90280            source[n1 + i] = s2;
 90281            source[n2 + i] = s;
 282        }
 283
 30284    }
 285
 286    private string GetHashed(string source)
 287    {
 120288        var sb = new StringBuilder();
 289
 120290        using var md5 = System.Security.Cryptography.MD5.Create();
 120291        var skip = 0;
 300292        while (sb.Length < source.Length)
 293        {
 180294            var s = source.Substring(skip, Math.Min(16, source.Length - skip));
 180295            var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(s));
 180296            var hashString = Convert.ToBase64String(hash).Replace("==", "").Replace("=", "");
 180297            sb.Append(hashString);
 298
 180299            skip++;
 300        }
 301
 120302        return sb.ToString().Substring(0, source.Length);
 120303    }
 304
 305    public static TargetGeneratorOperation ParseOperation(string s)
 306    {
 240307        var opString = s.Substring(0, 3);
 240308        if (!Enum.TryParse<OperationEnum>(opString, ignoreCase: true, out var op))
 0309            throw new InvalidDataException($"invalid operation {opString}");
 310
 240311        var args = s.Split(' ').Skip(1);
 312
 240313        return new TargetGeneratorOperation()
 240314        {
 240315            Op = op,
 240316            Args = args.ToArray()
 240317        };
 318    }
 319
 320    public static TargetGeneratorOperation[] Parse(string[] lines)
 321    {
 322
 150323        var operations = new List<TargetGeneratorOperation>();
 324
 920325        foreach(var line in lines)
 310326            if (!string.IsNullOrWhiteSpace(line))
 240327                operations.Add(ParseOperation(line.Trim()));
 328
 150329        return operations.ToArray();
 330    }
 331}
 332