Prompt:请用C#生成一个提供度量衡服务的实现类MeasurementService,它提供了以下方法将长度值转换为英寸、长度值转换为英尺、 将长度值转换为英里、 将长度值转换为厘米、 将长度值转换为千米、 将重量值转换为克、 将重量值转换为千克、 将功率值转换为瓦特、 将电流值转换为安培、 将电压值转换为伏特。例如将长度值转换为英寸的实现方法是public double ConvertToInch(double value, LengthUnit lengthUnit),这个方法中将变量LengthUnit,做长度转换。方法请添加标准注释,使用中文注释。
Prompt:MeasurementService类中,请参考ConvertToInch这个方法的实现方式和注释要求,继续生成ConvertToMile,ConvertToFoot,ConvertToCentimeter,ConvertToKilometer,ConvertToGram,ConvertToKilogram,ConvertToWatt,ConvertToAmpere,ConvertToVolt等方法的具体转换逻辑,每个方法都要实现代码输出
Repeat similar tests for other methods:ConvertToMile, ConvertToFoot, ConvertToCentimeter, ConvertToKilometer, ConvertToGram, ConvertToKilogram, ConvertToWatt, ConvertToAmpere, ConvertToVolt
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using T.Core.i10n.Service;
- using T.Core.i10n.SPI;
- namespace T.Core.i10n.Service.Tests
- {
- [TestClass]
- public class MeasurementServiceTests
- {
- private MeasurementService? _measurementService;
- [TestInitialize]
- public void Setup()
- {
- _measurementService = new MeasurementService();
- }
- [TestMethod]
- public void ConvertToInch_ShouldConvertCorrectly()
- {
- var result = _measurementService!.ConvertToInch(1, LengthUnit.Meter);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(39.3701, result, 0.0001);
- }
- [TestMethod]
- [ExpectedException(typeof(NotSupportedException))]
- public void ConvertToInch_ShouldThrowExceptionForUnsupportedUnit()
- {
- _measurementService!.ConvertToInch(1, (LengthUnit)999);
- }
- [TestMethod]
- public void ConvertToMile_ShouldConvertCorrectly()
- {
- var result = _measurementService!.ConvertToMile(1, LengthUnit.Kilometer);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.621371, result, 0.0001);
- }
- [TestMethod]
- [ExpectedException(typeof(NotSupportedException))]
- public void ConvertToMile_ShouldThrowExceptionForUnsupportedUnit()
- {
- _measurementService!.ConvertToMile(1, (LengthUnit)999);
- }
- [TestMethod]
- public void ConvertToFoot_ShouldConvertCorrectly()
- {
- var result = _measurementService!.ConvertToFoot(1, LengthUnit.Meter);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(3.28084, result, 0.0001);
- }
- [TestMethod]
- [ExpectedException(typeof(NotSupportedException))]
- public void ConvertToFoot_ShouldThrowExceptionForUnsupportedUnit()
- {
- _measurementService!.ConvertToFoot(1, (LengthUnit)999);
- }
- [TestMethod]
- public void ConvertToCentimeter_ShouldConvertCorrectly()
- {
- var result = _measurementService!.ConvertToCentimeter(1, LengthUnit.Meter);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(100, result, 0.0001);
- }
- [TestMethod]
- [ExpectedException(typeof(NotSupportedException))]
- public void ConvertToCentimeter_ShouldThrowExceptionForUnsupportedUnit()
- {
- _measurementService!.ConvertToCentimeter(1, (LengthUnit)999);
- }
- [TestMethod]
- public void ConvertToKilometer_ShouldConvertCorrectly()
- {
- var result = _measurementService!.ConvertToKilometer(1, LengthUnit.Meter);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.001, result, 0.0001);
- }
- [TestMethod]
- [ExpectedException(typeof(NotSupportedException))]
- public void ConvertToKilometer_ShouldThrowExceptionForUnsupportedUnit()
- {
- _measurementService!.ConvertToKilometer(1, (LengthUnit)999);
- }
- [TestMethod]
- public void ConvertToGram_ShouldConvertCorrectly()
- {
- var result = _measurementService!.ConvertToGram(1, WeightUnit.Kilogram);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1000, result, 0.0001);
- }
- [TestMethod]
- [ExpectedException(typeof(NotSupportedException))]
- public void ConvertToGram_ShouldThrowExceptionForUnsupportedUnit()
- {
- _measurementService!.ConvertToGram(1, (WeightUnit)999);
- }
- [TestMethod]
- public void ConvertToKilogram_ShouldConvertCorrectly()
- {
- var result = _measurementService!.ConvertToKilogram(1, WeightUnit.Gram);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.001, result, 0.0001);
- }
- [TestMethod]
- [ExpectedException(typeof(NotSupportedException))]
- public void ConvertToKilogram_ShouldThrowExceptionForUnsupportedUnit()
- {
- _measurementService!.ConvertToKilogram(1, (WeightUnit)999);
- }
- [TestMethod]
- public void ConvertToWatt_ShouldConvertCorrectly()
- {
- var result = _measurementService!.ConvertToWatt(1, PowerUnit.Kilowatt);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1000, result, 0.0001);
- }
- [TestMethod]
- [ExpectedException(typeof(NotSupportedException))]
- public void ConvertToWatt_ShouldThrowExceptionForUnsupportedUnit()
- {
- _measurementService!.ConvertToWatt(1, (PowerUnit)999);
- }
- [TestMethod]
- public void ConvertToAmpere_ShouldConvertCorrectly()
- {
- var result = _measurementService!.ConvertToAmpere(1, CurrentUnit.Milliampere);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.001, result, 0.0001);
- }
- [TestMethod]
- [ExpectedException(typeof(NotSupportedException))]
- public void ConvertToAmpere_ShouldThrowExceptionForUnsupportedUnit()
- {
- _measurementService!.ConvertToAmpere(1, (CurrentUnit)999);
- }
- [TestMethod]
- public void ConvertToVolt_ShouldConvertCorrectly()
- {
- var result = _measurementService!.ConvertToVolt(1, VoltageUnit.Millivolt);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.001, result, 0.0001);
- }
- [TestMethod]
- [ExpectedException(typeof(NotSupportedException))]
- public void ConvertToVolt_ShouldThrowExceptionForUnsupportedUnit()
- {
- _measurementService!.ConvertToVolt(1, (VoltageUnit)999);
- }
- [TestMethod]
- public void ConvertToInch_ShouldConvertCorrectly_ForEachLengthUnit()
- {
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToInch(1, LengthUnit.Inch), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(39.3701, _measurementService!.ConvertToInch(1, LengthUnit.Meter), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.393701, _measurementService!.ConvertToInch(1, LengthUnit.Centimeter), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(39370.1, _measurementService!.ConvertToInch(1, LengthUnit.Kilometer), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(63360, _measurementService!.ConvertToInch(1, LengthUnit.Mile), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(12, _measurementService!.ConvertToInch(1, LengthUnit.Foot), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(36, _measurementService!.ConvertToInch(1, LengthUnit.Yard), 0.0001);
- }
- [TestMethod]
- public void ConvertToMile_ShouldConvertCorrectly_ForEachLengthUnit()
- {
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToMile(1, LengthUnit.Mile), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0000062137, _measurementService!.ConvertToMile(1, LengthUnit.Centimeter), 0.0000000001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0000157828, _measurementService!.ConvertToMile(1, LengthUnit.Inch), 0.0000000001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.621371, _measurementService!.ConvertToMile(1, LengthUnit.Kilometer), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0005681818, _measurementService!.ConvertToMile(1, LengthUnit.Yard), 0.0000000001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0006213712, _measurementService!.ConvertToMile(1, LengthUnit.Meter), 0.0000000001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0001893939, _measurementService!.ConvertToMile(1, LengthUnit.Foot), 0.0000000001);
- }
- [TestMethod]
- public void ConvertToFoot_ShouldConvertCorrectly_ForEachLengthUnit()
- {
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToFoot(1, LengthUnit.Foot), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(3.28084, _measurementService!.ConvertToFoot(1, LengthUnit.Meter), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0328084, _measurementService!.ConvertToFoot(1, LengthUnit.Centimeter), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(3280.84, _measurementService!.ConvertToFoot(1, LengthUnit.Kilometer), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(5280, _measurementService!.ConvertToFoot(1, LengthUnit.Mile), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(3, _measurementService!.ConvertToFoot(1, LengthUnit.Yard), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0833333, _measurementService!.ConvertToFoot(1, LengthUnit.Inch), 0.0001);
- }
- [TestMethod]
- public void ConvertToCentimeter_ShouldConvertCorrectly_ForEachLengthUnit()
- {
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToCentimeter(1, LengthUnit.Centimeter), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(100, _measurementService!.ConvertToCentimeter(1, LengthUnit.Meter), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(2.54, _measurementService!.ConvertToCentimeter(1, LengthUnit.Inch), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(100000, _measurementService!.ConvertToCentimeter(1, LengthUnit.Kilometer), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(160934.4, _measurementService!.ConvertToCentimeter(1, LengthUnit.Mile), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(91.44, _measurementService!.ConvertToCentimeter(1, LengthUnit.Yard), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(30.48, _measurementService!.ConvertToCentimeter(1, LengthUnit.Foot), 0.0001);
- }
- // Continue with similar tests for the other methods...
- [TestMethod]
- public void ConvertToKilometer_ShouldConvertCorrectly_ForEachLengthUnit()
- {
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToKilometer(1, LengthUnit.Kilometer), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.001, _measurementService!.ConvertToKilometer(1, LengthUnit.Meter), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0000254, _measurementService!.ConvertToKilometer(1, LengthUnit.Inch), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1.609344, _measurementService!.ConvertToKilometer(1, LengthUnit.Mile), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0009144, _measurementService!.ConvertToKilometer(1, LengthUnit.Yard), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0003048, _measurementService!.ConvertToKilometer(1, LengthUnit.Foot), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.00001, _measurementService!.ConvertToKilometer(1, LengthUnit.Centimeter), 0.0001);
- }
- [TestMethod]
- public void ConvertToGram_ShouldConvertCorrectly_ForEachWeightUnit()
- {
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToGram(1, WeightUnit.Gram), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1000, _measurementService!.ConvertToGram(1, WeightUnit.Kilogram), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(453.59237, _measurementService!.ConvertToGram(1, WeightUnit.Pound), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1000000, _measurementService!.ConvertToGram(1, WeightUnit.Tonne), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(28.349523125, _measurementService!.ConvertToGram(1, WeightUnit.Ounce), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.001, _measurementService!.ConvertToGram(1, WeightUnit.Milligram), 0.0001);
- }
- [TestMethod]
- public void ConvertToKilogram_ShouldConvertCorrectly_ForEachWeightUnit()
- {
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToKilogram(1, WeightUnit.Kilogram), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.001, _measurementService!.ConvertToKilogram(1, WeightUnit.Gram), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.453592, _measurementService!.ConvertToKilogram(1, WeightUnit.Pound), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1000, _measurementService!.ConvertToKilogram(1, WeightUnit.Tonne), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0283495, _measurementService!.ConvertToKilogram(1, WeightUnit.Ounce), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.000001, _measurementService!.ConvertToKilogram(1, WeightUnit.Milligram), 0.0001);
- }
- [TestMethod]
- public void ConvertToWatt_ShouldConvertCorrectly_ForEachPowerUnit()
- {
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToWatt(1, PowerUnit.Watt), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1000, _measurementService!.ConvertToWatt(1, PowerUnit.Kilowatt), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(745.7, _measurementService!.ConvertToWatt(1, PowerUnit.Horsepower), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1000000, _measurementService!.ConvertToWatt(1, PowerUnit.Megawatt), 0.0001);
- }
- [TestMethod]
- public void ConvertToAmpere_ShouldConvertCorrectly_ForEachCurrentUnit()
- {
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToAmpere(1, CurrentUnit.Ampere), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.001, _measurementService!.ConvertToAmpere(1, CurrentUnit.Milliampere), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.000001, _measurementService!.ConvertToAmpere(1, CurrentUnit.Microampere), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1000, _measurementService!.ConvertToAmpere(1, CurrentUnit.Kiloampere), 0.0001);
- }
- [TestMethod]
- public void ConvertToVolt_ShouldConvertCorrectly_ForEachVoltageUnit()
- {
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToVolt(1, VoltageUnit.Volt), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.001, _measurementService!.ConvertToVolt(1, VoltageUnit.Millivolt), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.000001, _measurementService!.ConvertToVolt(1, VoltageUnit.Microvolt), 0.0001);
- Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1000, _measurementService!.ConvertToVolt(1, VoltageUnit.Kilovolt), 0.0001);
- }
- // Continue with similar tests for the other methods...
- }
- }