Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
namespace LeetCodeNet.G0301_0400.S0399_evaluate_division {

using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

public static class ArrayUtils {
public static IList<IList<string>> GetLists(string[][] array) {
var result = new List<IList<string>>();
foreach (var innerArray in array) {
result.Add(innerArray.ToList());
}
return result;
}
}

public class SolutionTest {
private const double Tolerance = 0.00001;

[Fact]
public void CalcEquation() {
IList<IList<string>> equations = ArrayUtils.GetLists(new string[][] {new string[] {"a", "b"}, new string[] {"b", "c"}});
double[] values = new double[] {2.0, 3.0};
IList<IList<string>> queries =
ArrayUtils.GetLists(
new string[][] {
new string[] {"a", "c"}, new string[] {"b", "a"}, new string[] {"a", "e"}, new string[] {"a", "a"}, new string[] {"x", "x"}
});
double[] expected = {6.00000, 0.50000, -1.00000, 1.00000, -1.00000};
AssertEqualWithTolerance(expected, new Solution().CalcEquation(equations, values, queries), Tolerance);
}

[Fact]
public void CalcEquation2() {
IList<IList<string>> equations =
ArrayUtils.GetLists(new string[][] {new string[] {"a", "b"}, new string[] {"b", "c"}, new string[] {"bc", "cd"}});
double[] values = new double[] {1.5, 2.5, 5.0};
IList<IList<string>> queries =
ArrayUtils.GetLists(
new string[][] {new string[] {"a", "c"}, new string[] {"c", "b"}, new string[] {"bc", "cd"}, new string[] {"cd", "bc"}});
double[] expected = {3.75000, 0.40000, 5.00000, 0.20000};
AssertEqualWithTolerance(expected, new Solution().CalcEquation(equations, values, queries), Tolerance);
}

[Fact]
public void CalcEquation3() {
IList<IList<string>> equations = new List<IList<string>> {
new List<string> {"a", "b"}
};
double[] values = new double[] {0.5};
IList<IList<string>> queries =
ArrayUtils.GetLists(
new string[][] {new string[] {"a", "b"}, new string[] {"b", "a"}, new string[] {"a", "c"}, new string[] {"x", "y"}});
double[] expected = {0.50000, 2.00000, -1.00000, -1.00000};
AssertEqualWithTolerance(expected, new Solution().CalcEquation(equations, values, queries), Tolerance);
}

private static void AssertEqualWithTolerance(double[] expected, double[] actual, double tolerance) {
Assert.Equal(expected.Length, actual.Length);
for (int i = 0; i < expected.Length; i++) {
Assert.InRange(actual[i], expected[i] - tolerance, expected[i] + tolerance);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace LeetCodeNet.G0501_0600.S0530_minimum_absolute_difference_in_bst {

using System;
using LeetCodeNet.Com_github_leetcode;
using Xunit;

public class SolutionTest {
[Fact]
public void GetMinimumDifference() {
TreeNode treeNode = TreeUtils.ConstructBinaryTree(new List<int?> {4, 2, 6, 1, 3});
Assert.Equal(1, new Solution().GetMinimumDifference(treeNode));
}

[Fact]
public void GetMinimumDifference2() {
TreeNode treeNode =
TreeUtils.ConstructBinaryTree(new List<int?> {1, 0, 48, null, null, 12, 49});
Assert.Equal(1, new Solution().GetMinimumDifference(treeNode));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace LeetCodeNet.G0601_0700.S0637_average_of_levels_in_binary_tree {

using System;
using System.Collections.Generic;
using System.Linq;
using LeetCodeNet.Com_github_leetcode;
using Xunit;

public class SolutionTest {
[Fact]
public void AverageOfLevels() {
TreeNode treeNode = TreeNode.Create(new List<int?> { 3, 9, 20, null, null, 15, 7 });
var expected = new List<double> { 3.00000, 14.50000, 11.00000 };
var actual = new Solution().AverageOfLevels(treeNode);
AssertEqualWithTolerance(expected, actual, 0.00001);
}

[Fact]
public void AverageOfLevels2() {
TreeNode treeNode = TreeNode.Create(new List<int?> { 3, 9, 20, 15, 7 });
var expected = new List<double> { 3.00000, 14.50000, 11.00000 };
var actual = new Solution().AverageOfLevels(treeNode);
AssertEqualWithTolerance(expected, actual, 0.00001);
}

private void AssertEqualWithTolerance(IList<double> expected, IList<double> actual, double tolerance) {
Assert.Equal(expected.Count, actual.Count);
for (var i = 0; i < expected.Count; i++) {
Assert.True(Math.Abs(expected[i] - actual[i]) <= tolerance,
$"Mismatch at index {i}: expected {expected[i]}, actual {actual[i]}");
}
}
}
}
Loading