< Summary

Information
Class: DeltaSharp.TargetGenerator
Assembly: DeltaSharp
File(s): /home/runner/work/DeltaSharp/DeltaSharp/src/DeltaSharp/TestHelper/TargetGenerator.cs
Line coverage
91%
Covered lines: 21
Uncovered lines: 2
Coverable lines: 23
Total lines: 332
Line coverage: 91.3%
Branch coverage
75%
Covered branches: 6
Total branches: 8
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Source()100%11100%
get_SourceLines()100%11100%
.ctor(...)50%4471.42%
.ctor(...)100%22100%
Execute(...)100%11100%
Execute(...)100%22100%

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{
 2013    public byte[] Source => Encoding.UTF8.GetBytes(string.Join("\n", SourceLines) + "\n");
 14
 72015    public List<string> SourceLines { get; private set; }
 16
 17    Random _random;
 18
 2019    public TargetGenerator(string? source = null, Random? random=null)
 20    {
 2021        _random = random ?? new Random();
 22
 2023        if (source is not null)
 24        {
 025            var lines = source.Split('\n').Select(l => l.TrimEnd('\r')).ToList();
 026            SourceLines = lines;
 27        }
 28        else
 29        {
 2030            SourceLines = new List<string>();
 31        }
 2032    }
 33
 13034    public TargetGenerator(IEnumerable<string> lines, Random? random = null)
 35    {
 13036        _random = random ?? new Random();
 37
 13038        SourceLines = lines.ToList();
 13039    }
 40
 41    public void Execute(string input)
 42    {
 36043        var lines = input.Split('\n').Select(l => l.TrimEnd('\r')).ToArray();
 44
 67045        lines = lines.SelectMany(l => l.Split(';')).Select(l => l.Trim()).ToArray();
 46
 15047        var target = new List<string>(SourceLines);
 48
 15049        var operations = TargetGeneratorOperation.Parse(lines);
 50
 15051        Execute(operations);
 52
 15053    }
 54
 55    public void Execute(TargetGeneratorOperation[] operations)
 56    {
 15057        var target = SourceLines;
 58
 78059        foreach (var op in operations)
 24060            op.Apply(target,_random);
 61
 15062    }
 63
 64}
 65
 66internal enum OperationEnum
 67{
 68    Set,
 69    Add,
 70    Del,
 71    Swp
 72}
 73
 74internal class TargetGeneratorOperation
 75{
 76    public OperationEnum Op { get; set; }
 77
 78    public string[] Args { get; set; } = Array.Empty<string>();
 79
 80
 81    private TargetGeneratorOperation()
 82    {
 83    }
 84
 85    public void Apply(List<string> source,Random random)
 86    {
 87        switch (Op)
 88        {
 89            case OperationEnum.Add: ApplyAdd(source, random); break;
 90            case OperationEnum.Del: ApplyDel(source); break;
 91            case OperationEnum.Set: ApplySet(source, random); break;
 92            case OperationEnum.Swp: ApplySwap(source); break;
 93        }
 94    }
 95
 96    private void ApplyAdd(List<string> source,Random random)
 97    {
 98        if (Args.Length <1)
 99            throw new InvalidDataException("[ApplyAdd] invalid argument count");
 100
 101        if (!int.TryParse(Args[0],out var position))
 102            throw new InvalidDataException("[ApplyAdd] invalid position");
 103
 104        if (position > source.Count - 1 || position == -1)
 105            position = source.Count;
 106
 107        var buffer = new byte[16];
 108
 109        var count = 1;
 110        var data = string.Empty;
 111        if (Args.Length > 1)
 112        {
 113            data = Args[1];
 114
 115            if (Args.Length > 2)
 116            {
 117                if (!int.TryParse(Args[2], out count))
 118                    throw new InvalidDataException("[ApplySet] invalid count");
 119            }
 120        }
 121        else
 122        {
 123            random.NextBytes(buffer);
 124
 125            data = Convert.ToBase64String(buffer, Base64FormattingOptions.None)
 126                    .Replace("==", "").Replace("=", "");
 127        }
 128
 129
 130        while (count > 0)
 131        {
 132            var s = data;
 133            if (data == "RND")
 134            {
 135                random.NextBytes(buffer);
 136
 137                s = Convert.ToBase64String(buffer, Base64FormattingOptions.None)
 138                    .Replace("==", "").Replace("=", "");
 139
 140            }
 141            else if (data.StartsWith("RND(") && data.EndsWith(")"))
 142            {
 143                var numberString = data.Replace("RND(", "").Replace(")", "");
 144                var randCount = int.Parse(numberString);
 145
 146                var tmp = new byte[randCount];
 147                for (var i = 0; i < tmp.Length; i++)
 148                    tmp[i] = (byte)random.Next(32, 127);
 149
 150                s = Encoding.UTF8.GetString(tmp).Replace('\r', 'r').Replace('\n', 'n');
 151            }
 152
 153            source.Insert(position, s);
 154            count--;
 155        }
 156
 157    }
 158
 159    private void ApplySet(List<string> source,Random random)
 160    {
 161        if (Args.Length < 1)
 162            throw new InvalidDataException("[ApplySet] invalid argument count");
 163
 164        if (!int.TryParse(Args[0], out var position))
 165            throw new InvalidDataException("[ApplySet] invalid position");
 166
 167        if(position>source.Count-1)
 168            throw new InvalidDataException("[ApplySet] invalid position");
 169
 170        var buffer = new byte[16];
 171
 172        int count = 1;
 173        int step = 1;
 174        var data = string.Empty;
 175
 176        if (Args.Length > 1)
 177        {
 178            data = Args[1];
 179
 180            if (Args.Length > 2)
 181            {
 182                if (!int.TryParse(Args[2], out count))
 183                    throw new InvalidDataException("[ApplySet] invalid count");
 184            }
 185
 186            if (Args.Length > 3)
 187            {
 188                if (!int.TryParse(Args[3], out step))
 189                    throw new InvalidDataException("[ApplySet] invalid step");
 190            }
 191        }
 192        else
 193        {
 194
 195            random.NextBytes(buffer);
 196
 197            data = Convert.ToBase64String(buffer, Base64FormattingOptions.None)
 198                    .Replace("==", "").Replace("=", "");
 199        }
 200
 201        while(count > 0)
 202        {
 203            var i = position + (count - 1) * step;
 204
 205            var s = data;
 206            if (data == "RND")
 207            {
 208                random.NextBytes(buffer);
 209                s = Convert.ToBase64String(buffer, Base64FormattingOptions.None)
 210                   .Replace("==", "").Replace("=", "");
 211            }
 212            else if (data.StartsWith("RND(") && data.EndsWith(")"))
 213            {
 214                var numberString = data.Replace("RND(", "").Replace(")", "");
 215                var randCount = int.Parse(numberString);
 216
 217                var tmp = new byte[randCount];
 218                for (var z = 0; z < tmp.Length; z++)
 219                    tmp[z] = (byte)random.Next(32, 127);
 220
 221                s = Encoding.UTF8.GetString(tmp).Replace('\r', 'r').Replace('\n', 'n');
 222            }
 223            else if (data == "HASH")
 224            {
 225                var current = source[i];
 226                s = GetHashed(current);
 227            }
 228
 229            source[i] = s;
 230
 231            count--;
 232        }
 233    }
 234
 235    private void ApplyDel(List<string> source)
 236    {
 237        if (Args.Length < 1)
 238            throw new InvalidDataException("[ApplyDel] invalid argument count");
 239
 240        if (!int.TryParse(Args[0], out var position))
 241            throw new InvalidDataException("[ApplyDel] invalid position");
 242
 243        var count = 1;
 244        if (Args.Length > 1)
 245        {
 246            if (!int.TryParse(Args[1], out count))
 247                throw new InvalidDataException("[ApplyDel] invalid count");
 248        }
 249
 250        source.RemoveRange(position, count);
 251    }
 252
 253    private void ApplySwap(List<string> source)
 254    {
 255        if (Args.Length < 2)
 256            throw new InvalidDataException("[ApplySwap] invalid argument count");
 257
 258        if (!int.TryParse(Args[0], out var n1))
 259            throw new InvalidDataException("[ApplySwap] invalid position 1");
 260
 261        if (!int.TryParse(Args[1], out var n2))
 262            throw new InvalidDataException("[ApplySwap] invalid position 2");
 263
 264        int n3 = 1;
 265        if(Args.Length > 2)
 266        {
 267            //swap block
 268
 269            if (!int.TryParse(Args[2], out n3))
 270                throw new InvalidDataException("[ApplySwap] invalid position 2");
 271
 272        }
 273        if (n2 + n3 > source.Count || n1 + n3 > source.Count)
 274            throw new InvalidDataException("[ApplySwap] invalid range");
 275
 276        foreach (var i in Enumerable.Range(0, n3))
 277        {
 278            var s = source[n1 + i];
 279            var s2 = source[n2 + i];
 280            source[n1 + i] = s2;
 281            source[n2 + i] = s;
 282        }
 283
 284    }
 285
 286    private string GetHashed(string source)
 287    {
 288        var sb = new StringBuilder();
 289
 290        using var md5 = System.Security.Cryptography.MD5.Create();
 291        var skip = 0;
 292        while (sb.Length < source.Length)
 293        {
 294            var s = source.Substring(skip, Math.Min(16, source.Length - skip));
 295            var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(s));
 296            var hashString = Convert.ToBase64String(hash).Replace("==", "").Replace("=", "");
 297            sb.Append(hashString);
 298
 299            skip++;
 300        }
 301
 302        return sb.ToString().Substring(0, source.Length);
 303    }
 304
 305    public static TargetGeneratorOperation ParseOperation(string s)
 306    {
 307        var opString = s.Substring(0, 3);
 308        if (!Enum.TryParse<OperationEnum>(opString, ignoreCase: true, out var op))
 309            throw new InvalidDataException($"invalid operation {opString}");
 310
 311        var args = s.Split(' ').Skip(1);
 312
 313        return new TargetGeneratorOperation()
 314        {
 315            Op = op,
 316            Args = args.ToArray()
 317        };
 318    }
 319
 320    public static TargetGeneratorOperation[] Parse(string[] lines)
 321    {
 322
 323        var operations = new List<TargetGeneratorOperation>();
 324
 325        foreach(var line in lines)
 326            if (!string.IsNullOrWhiteSpace(line))
 327                operations.Add(ParseOperation(line.Trim()));
 328
 329        return operations.ToArray();
 330    }
 331}
 332