Commit d0ae5dc1 authored by shrabbit's avatar shrabbit
Browse files

Add RunProgram and MakeSpace playgrounds, implement Storage methods, and update assembly version

parent 55a32d1d
Loading
Loading
Loading
Loading
+113 −5
Original line number Diff line number Diff line
namespace TestV.Playgrounds;
using Spectre.Console;
using TestV.Tools;

namespace TestV.Playgrounds;

public class MakeSpace : IPlayground
{
    public string LaunchCmd { get; }
    public string? Name { get; }
    public string? Description { get; }
    public string LaunchCmd { get; } = "mkspace";
    public string? Name { get; } = nameof(MakeSpace);
    public string? Description { get; } = "占用存储空间";
    public async Task Run(string[] args)
    {
        throw new NotImplementedException();
        if (args.Length == 0 || args[0] is "help" or "/?")
        {
            ShowHelp();
            return;
        }

        switch (args.Length)
        {
            case < 2:
                AnsiConsole.MarkupLine("[red]错误:参数太少。[/]");
                return;
            case > 3:
                AnsiConsole.MarkupLine("[red]错误:参数太多。[/]");
                return;
            default:
                try
                {
                    var path = args[0];
                    var size = Storage.Parse(args[1], null);
                    var write = args.Length > 2 ? args[2].ToLower() : string.Empty;

                    Func<byte> writeDelegate = () => 0;

                    switch (write)
                    {
                        case "ff":
                            writeDelegate = () => 0xFF;
                            break;
                        
                        case "00":
                            writeDelegate = () => 0x0;
                            break;
                        
                        default:
                            if (write == string.Empty)
                                break;
                            
                            if (write[..2] == "rr")
                            {
                                var rnd = new Random();

                                if (write.Length > 2)
                                {
                                    rnd = new Random(int.Parse(write[2..], null));
                                }

                                writeDelegate = () => (byte)rnd.Next(0, 256);
                            }
                            else
                            {
                                AnsiConsole.MarkupLine($"[red]错误:不支持该写入方法[/]");
                                return;
                            }
                            break;
                    }
                    
                    AnsiConsole.MarkupLine($"[yellow]开始写入……\n文件:{path},大小:{size}[/]");
                    StartWrite(path, size, writeDelegate);
                }
                catch (Exception e)
                {
                    AnsiConsole.MarkupLine($"[red]错误:{e.Message}[/]");
                }

                break;
        }
    }

    private void ShowHelp()
    {
        AnsiConsole.WriteLine("参数1:写入文件路径;参数2:字节数;可选参数3:填充模式(00:全填0、ff:全填255、rr[seed]:随机值,后可选跟随机数种子)");
    }

    void StartWrite(string path, Storage size, Func<byte> write)
    {
        try
        {
            // 缓冲区大小:4MB (4 * 1024 * 1024 = 4194304 字节)
            // 这个大小既能保证 I/O 高效,又不会占用过多内存
            const int bufferSize = 4 * 1024 * 1024;
            byte[] buffer = new byte[bufferSize];

            using var stream = new FileStream(path, FileMode.CreateNew, FileAccess.Write, FileShare.None, bufferSize);

            long totalBytesToWrite = size.ToInt64(); // 使用 long 替代 decimal 进行循环
            long bytesWritten = 0;

            while (bytesWritten < totalBytesToWrite)
            {
                // 计算本次需要写入的字节数(处理最后不满 4MB 的零头)
                int bytesToWrite = (int)Math.Min(bufferSize, totalBytesToWrite - bytesWritten);

                for (int i = 0; i < bytesToWrite; i++)
                {
                    buffer[i] = write();
                }

                // 一次性写入整块数据
                stream.Write(buffer, 0, bytesToWrite);
                bytesWritten += bytesToWrite;
            }
        }
        catch (Exception e)
        {
            AnsiConsole.MarkupLine($"[red]写入失败:{e.Message}[/]");
        }
    }
}
 No newline at end of file
+25 −0
Original line number Diff line number Diff line
using System.Diagnostics;
using Spectre.Console;

namespace TestV.Playgrounds;

public class RunProgram : IPlayground
{
    public string LaunchCmd { get; } = "run";
    public string? Name { get; } = "Run Program";
    public string? Description { get; } = "原生运行";

    public async Task Run(string[] args)
    {
        if (args.Length == 0)
        {
            AnsiConsole.MarkupLine("[red]错误:参数太少。[/]");
            return;
        }

        var exec = args[0];
        var runArgs = args.Length > 1 ? args[1..] : [];
        
        await Process.Start(exec, runArgs).WaitForExitAsync();
    }
}
 No newline at end of file
+2 −0
Original line number Diff line number Diff line
@@ -147,6 +147,8 @@ class Program
                .Add<MaxFp64>()
                .Add<MaxDec>()
                .Add<StorageTest>()
                .Add<MakeSpace>()
                .Add<RunProgram>()
                .Add<Exp1>();
            
            play.Startup(args);
+98 −99
Original line number Diff line number Diff line
using System.Diagnostics.CodeAnalysis;
using System.Collections;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Numerics;

@@ -35,6 +36,7 @@ public struct Storage :

    public static Storage operator +(Storage value) => new Storage(+value._valueDecimal);


    public override string ToString()
    {
        return ToString("F2", CultureInfo.CurrentUICulture);
@@ -216,12 +218,12 @@ public struct Storage :

    public static bool operator <=(Storage left, Storage right) => left._valueDecimal <= right._valueDecimal;

    public static Storage operator --(Storage value) => new Storage(value._valueDecimal--);
    public static Storage operator --(Storage value) => new Storage(value._valueDecimal - 1);

    public static Storage operator /(Storage left, Storage right) => 
        new Storage(left._valueDecimal / right._valueDecimal);

    public static Storage operator ++(Storage value) => new Storage(value._valueDecimal++);
    public static Storage operator ++(Storage value) => new Storage(value._valueDecimal + 1);

    public static Storage MultiplicativeIdentity { get; } = new Storage(1);

@@ -230,120 +232,61 @@ public struct Storage :
    public static Storage operator -(Storage left, Storage right) =>
        new Storage(left._valueDecimal - right._valueDecimal);

    public static Storage operator -(Storage value)
    {
        throw new NotImplementedException();
    }
    public static Storage operator -(Storage value) => new Storage(-value._valueDecimal);

    public static Storage Abs(Storage value)
    {
        throw new NotImplementedException();
    }
    public static Storage Abs(Storage value) => new Storage(Math.Abs(value._valueDecimal));

    public static bool IsCanonical(Storage value)
    {
        throw new NotImplementedException();
    }
    public static bool IsCanonical(Storage value) => true;

    public static bool IsComplexNumber(Storage value)
    {
        throw new NotImplementedException();
    }
    public static bool IsComplexNumber(Storage value) => false;

    public static bool IsEvenInteger(Storage value)
    {
        throw new NotImplementedException();
        var intPart = decimal.Truncate(value._valueDecimal);
        return intPart % 2 == 0;
    }

    public static bool IsFinite(Storage value)
    {
        throw new NotImplementedException();
    }
    public static bool IsFinite(Storage value) => true;

    public static bool IsImaginaryNumber(Storage value)
    {
        throw new NotImplementedException();
    }
    public static bool IsImaginaryNumber(Storage value) => false;

    public static bool IsInfinity(Storage value)
    {
        throw new NotImplementedException();
    }
    public static bool IsInfinity(Storage value) => false;

    public static bool IsInteger(Storage value)
    {
        throw new NotImplementedException();
    }
    public static bool IsInteger(Storage value) => decimal.Truncate(value._valueDecimal) == value._valueDecimal;

    public static bool IsNaN(Storage value)
    {
        throw new NotImplementedException();
    }
    public static bool IsNaN(Storage value) => false;

    public static bool IsNegative(Storage value)
    {
        throw new NotImplementedException();
    }
    public static bool IsNegative(Storage value) => value._valueDecimal < 0;

    public static bool IsNegativeInfinity(Storage value)
    {
        throw new NotImplementedException();
    }
    public static bool IsNegativeInfinity(Storage value) => false;

    public static bool IsNormal(Storage value)
    {
        throw new NotImplementedException();
    }
    public static bool IsNormal(Storage value) => value._valueDecimal != 0;

    public static bool IsOddInteger(Storage value)
    {
        throw new NotImplementedException();
        var intPart = decimal.Truncate(value._valueDecimal);
        return intPart % 2 != 0;
    }

    public static bool IsPositive(Storage value)
    {
        throw new NotImplementedException();
    }
    public static bool IsPositive(Storage value) => value._valueDecimal > 0;

    public static bool IsPositiveInfinity(Storage value)
    {
        throw new NotImplementedException();
    }
    public static bool IsPositiveInfinity(Storage value) => false;

    public static bool IsRealNumber(Storage value)
    {
        throw new NotImplementedException();
    }
    public static bool IsRealNumber(Storage value) => true;

    public static bool IsSubnormal(Storage value)
    {
        throw new NotImplementedException();
    }
    public static bool IsSubnormal(Storage value) => false;

    public static bool IsZero(Storage value)
    {
        throw new NotImplementedException();
    }
    public static bool IsZero(Storage value) => value._valueDecimal == 0;

    public static Storage MaxMagnitude(Storage x, Storage y)
    {
        throw new NotImplementedException();
    }
    public static Storage MaxMagnitude(Storage x, Storage y) =>
        Math.Abs(x._valueDecimal) >= Math.Abs(y._valueDecimal) ? x : y;

    public static Storage MaxMagnitudeNumber(Storage x, Storage y)
    {
        throw new NotImplementedException();
    }
    public static Storage MaxMagnitudeNumber(Storage x, Storage y) => MaxMagnitude(x, y);

    public static Storage MinMagnitude(Storage x, Storage y)
    {
        throw new NotImplementedException();
    }
    public static Storage MinMagnitude(Storage x, Storage y) =>
        Math.Abs(x._valueDecimal) <= Math.Abs(y._valueDecimal) ? x : y;

    public static Storage MinMagnitudeNumber(Storage x, Storage y)
    {
        throw new NotImplementedException();
    }
    public static Storage MinMagnitudeNumber(Storage x, Storage y) => MinMagnitude(x, y);

    public static Storage Parse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider? provider)
    {
@@ -361,32 +304,88 @@ public struct Storage :

    public static bool TryConvertFromChecked<TOther>(TOther value, out Storage result) where TOther : INumberBase<TOther>
    {
        throw new NotImplementedException();
        if (typeof(TOther) == typeof(decimal))
        {
            result = new Storage(decimal.CreateChecked(value));
            return true;
        }
        if (typeof(TOther) == typeof(double))
        {
            result = new Storage((decimal)double.CreateChecked(value));
            return true;
        }
        if (typeof(TOther) == typeof(float))
        {
            result = new Storage((decimal)float.CreateChecked(value));
            return true;
        }
        if (typeof(TOther) == typeof(int))
        {
            result = new Storage(int.CreateChecked(value));
            return true;
        }
        if (typeof(TOther) == typeof(long))
        {
            result = new Storage(long.CreateChecked(value));
            return true;
        }
        result = default;
        return false;
    }

    public static bool TryConvertFromSaturating<TOther>(TOther value, out Storage result) where TOther : INumberBase<TOther>
    {
        throw new NotImplementedException();
        result = new Storage(decimal.CreateSaturating(value));
        return true;
    }

    public static bool TryConvertFromTruncating<TOther>(TOther value, out Storage result) where TOther : INumberBase<TOther>
    {
        throw new NotImplementedException();
        result = new Storage(decimal.CreateTruncating(value));
        return true;
    }

    public static bool TryConvertToChecked<TOther>(Storage value, [MaybeNullWhen(false)] out TOther result) where TOther : INumberBase<TOther>
    {
        throw new NotImplementedException();
        if (typeof(TOther) == typeof(decimal))
        {
            result = (TOther)(object)value._valueDecimal;
            return true;
        }
        if (typeof(TOther) == typeof(double))
        {
            result = (TOther)(object)(double)value._valueDecimal;
            return true;
        }
        if (typeof(TOther) == typeof(float))
        {
            result = (TOther)(object)(float)value._valueDecimal;
            return true;
        }
        if (typeof(TOther) == typeof(int))
        {
            result = (TOther)(object)(int)value._valueDecimal;
            return true;
        }
        if (typeof(TOther) == typeof(long))
        {
            result = (TOther)(object)(long)value._valueDecimal;
            return true;
        }
        result = default;
        return false;
    }

    public static bool TryConvertToSaturating<TOther>(Storage value, [MaybeNullWhen(false)] out TOther result) where TOther : INumberBase<TOther>
    {
        throw new NotImplementedException();
        result = (TOther)(object)decimal.CreateSaturating(value._valueDecimal);
        return true;
    }

    public static bool TryConvertToTruncating<TOther>(Storage value, [MaybeNullWhen(false)] out TOther result) where TOther : INumberBase<TOther>
    {
        throw new NotImplementedException();
        result = (TOther)(object)decimal.CreateTruncating(value._valueDecimal);
        return true;
    }

    public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider? provider, out Storage result)
@@ -438,7 +437,7 @@ public struct Storage :
        return TryParse(s.AsSpan(), style, provider, out result);
    }

    public static Storage One { get; }
    public static int Radix { get; }
    public static Storage Zero { get; }
    public static Storage One { get; } = new Storage(1);
    public static int Radix { get; } = 10;
    public static Storage Zero { get; } = new Storage(0);
}
 No newline at end of file
+1 −1
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("TestV")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+55a32d1dbe18ec418dc7f5a1ba077a8e3f79f110")]
[assembly: System.Reflection.AssemblyProductAttribute("TestV")]
[assembly: System.Reflection.AssemblyTitleAttribute("TestV")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
Loading