Skip to content

Commit

Permalink
Merge pull request #43 from MrTrueChina/3.0-测试功能转移到namespace下
Browse files Browse the repository at this point in the history
移动所有代码到 namespace 下
  • Loading branch information
MrTrueChina authored Jul 15, 2021
2 parents 1e6f201 + 6c240b5 commit f00f582
Show file tree
Hide file tree
Showing 17 changed files with 399 additions and 313 deletions.
23 changes: 13 additions & 10 deletions Assets/Quadtree Collider Detection/MyGizmos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@
using System.Collections.Generic;
using UnityEngine;

public static partial class MyGizmos
namespace MtC.Tools.QuadtreeCollider
{
public static void DrawCircle(Vector3 center, float radius, int edgeNumber = 360)
public static partial class MyGizmos
{
Vector3 beginPoint = center + Vector3.right * radius; //三角函数角度是从正右方开始的,画圆起始点是最右边的点
for (int i = 1; i <= edgeNumber; i++)
public static void DrawCircle(Vector3 center, float radius, int edgeNumber = 360)
{
float angle = 2 * Mathf.PI / edgeNumber * i;
Vector3 beginPoint = center + Vector3.right * radius; //三角函数角度是从正右方开始的,画圆起始点是最右边的点
for (int i = 1; i <= edgeNumber; i++)
{
float angle = 2 * Mathf.PI / edgeNumber * i;

float x = radius * Mathf.Cos(angle) + center.x;
float y = radius * Mathf.Sin(angle) + center.y;
Vector3 endPoint = new Vector3(x, y, center.z);
float x = radius * Mathf.Cos(angle) + center.x;
float y = radius * Mathf.Sin(angle) + center.y;
Vector3 endPoint = new Vector3(x, y, center.z);

Gizmos.DrawLine(beginPoint, endPoint);
Gizmos.DrawLine(beginPoint, endPoint);

beginPoint = endPoint;
beginPoint = endPoint;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,95 +5,99 @@
using NUnit.Framework;
using System.Text;

[TestFixture]
public class DictionaryTest

namespace MtC.Tools.QuadtreeCollider.Test
{
[Test]
public void UnionTest()
[TestFixture]
public class DictionaryTest
{
// 主字典
Dictionary<int, string> mainDictionary = new Dictionary<int, string>();
// 副字典
Dictionary<int, string> subDictionary = new Dictionary<int, string>();
[Test]
public void UnionTest()
{
// 主字典
Dictionary<int, string> mainDictionary = new Dictionary<int, string>();
// 副字典
Dictionary<int, string> subDictionary = new Dictionary<int, string>();

mainDictionary.Add(1, "One");
mainDictionary.Add(2, "Two");
mainDictionary.Add(1, "One");
mainDictionary.Add(2, "Two");

subDictionary.Add(3, "Three");
subDictionary.Add(3, "Three");

// 没有重复内容的 Union
mainDictionary = mainDictionary.Union(subDictionary).ToDictionary(pair => pair.Key, pair => pair.Value);
Debug.Log("没有重复的 Union:" + PrintDictionary(mainDictionary));
// 没有重复内容的 Union
mainDictionary = mainDictionary.Union(subDictionary).ToDictionary(pair => pair.Key, pair => pair.Value);
Debug.Log("没有重复的 Union:" + PrintDictionary(mainDictionary));

// 有重复的 Union
mainDictionary = mainDictionary.Union(subDictionary).ToDictionary(pair => pair.Key, pair => pair.Value);
Debug.Log("重复合并了 3 的 Union:" + PrintDictionary(mainDictionary));
// 有重复的 Union
mainDictionary = mainDictionary.Union(subDictionary).ToDictionary(pair => pair.Key, pair => pair.Value);
Debug.Log("重复合并了 3 的 Union:" + PrintDictionary(mainDictionary));

// 合并修改了的内容
subDictionary[3] = "NEW THREE";
mainDictionary = mainDictionary.Union(subDictionary).ToDictionary(pair => pair.Key, pair => pair.Value);
Debug.Log("合并了修改后的 3 的 Union:" + PrintDictionary(mainDictionary));
// 合并修改了的内容
subDictionary[3] = "NEW THREE";
mainDictionary = mainDictionary.Union(subDictionary).ToDictionary(pair => pair.Key, pair => pair.Value);
Debug.Log("合并了修改后的 3 的 Union:" + PrintDictionary(mainDictionary));

// 合并时如果有相同的 Key 不同值的情况会报错,这个方法不是很好用
}
// 合并时如果有相同的 Key 不同值的情况会报错,这个方法不是很好用
}

[Test]
public void RepeatAdd()
{
Dictionary<int, string> dictionary = new Dictionary<int, string>();
[Test]
public void RepeatAdd()
{
Dictionary<int, string> dictionary = new Dictionary<int, string>();

// 存入新的值
dictionary.Add(1, "One");
Debug.Log("存入新值:" + PrintDictionary(dictionary));
// 存入新的值
dictionary.Add(1, "One");
Debug.Log("存入新值:" + PrintDictionary(dictionary));

// 存入重复值
dictionary.Add(1, "One");
Debug.Log("存入重复值:" + PrintDictionary(dictionary));
// 存入重复值
dictionary.Add(1, "One");
Debug.Log("存入重复值:" + PrintDictionary(dictionary));

// 存入重复 Key 的不同值
dictionary.Add(1, "NEW ONE");
Debug.Log("存入重复 Key 的不同值" + PrintDictionary(dictionary));
// 存入重复 Key 的不同值
dictionary.Add(1, "NEW ONE");
Debug.Log("存入重复 Key 的不同值" + PrintDictionary(dictionary));

// 只要存入已有的 Key 就会报错
}
// 只要存入已有的 Key 就会报错
}

[Test]
public void SetByIndex()
{
Dictionary<int, string> dictionary = new Dictionary<int, string>();
[Test]
public void SetByIndex()
{
Dictionary<int, string> dictionary = new Dictionary<int, string>();

// 存入新的值
dictionary[1] = "One";
Debug.Log("存入新值:" + PrintDictionary(dictionary));
// 存入新的值
dictionary[1] = "One";
Debug.Log("存入新值:" + PrintDictionary(dictionary));

// 存入重复值
dictionary[1] = "One";
Debug.Log("存入重复值:" + PrintDictionary(dictionary));
// 存入重复值
dictionary[1] = "One";
Debug.Log("存入重复值:" + PrintDictionary(dictionary));

// 存入重复 Key 的不同值
dictionary[1] = "NEW ONE";
Debug.Log("存入重复 Key 的不同值" + PrintDictionary(dictionary));
// 存入重复 Key 的不同值
dictionary[1] = "NEW ONE";
Debug.Log("存入重复 Key 的不同值" + PrintDictionary(dictionary));

// 使用索引方式存值时,如果没有这个索引会添加,有这个索引会覆盖
}
// 使用索引方式存值时,如果没有这个索引会添加,有这个索引会覆盖
}

private string PrintDictionary<TKey, TVal>(Dictionary<TKey, TVal> dictionary)
{
StringBuilder stringBuilder = new StringBuilder();
private string PrintDictionary<TKey, TVal>(Dictionary<TKey, TVal> dictionary)
{
StringBuilder stringBuilder = new StringBuilder();

stringBuilder.Append("{");
stringBuilder.Append("{");

foreach(KeyValuePair<TKey,TVal> pair in dictionary)
{
stringBuilder.Append("(");
stringBuilder.Append(pair.Key.ToString());
stringBuilder.Append(", ");
stringBuilder.Append(pair.Value.ToString());
stringBuilder.Append("), ");
}
foreach (KeyValuePair<TKey, TVal> pair in dictionary)
{
stringBuilder.Append("(");
stringBuilder.Append(pair.Key.ToString());
stringBuilder.Append(", ");
stringBuilder.Append(pair.Value.ToString());
stringBuilder.Append("), ");
}

stringBuilder.Append("}");
stringBuilder.Append("}");

return stringBuilder.ToString();
return stringBuilder.ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,67 @@
using NUnit.Framework;
using System;

/// <summary>
/// 事件委托的订阅机制测试
/// </summary>
[TestFixture]
public class SubscribeTest
namespace MtC.Tools.QuadtreeCollider.Test
{
private Action<string> actions;

[TearDown]
public void TearDown()
{
if (actions != null)
foreach (Action<string> a in actions.GetInvocationList())
actions -= a;
}

[Test]
public void RepeatSubscribe()
/// <summary>
/// 事件委托的订阅机制测试
/// </summary>
[TestFixture]
public class SubscribeTest
{
actions += Say;
actions += Say;
actions += Say;

Debug.Log(actions.GetInvocationList().Length == 1 ? "重复订阅不会产生多个订阅" : "重复订阅会产生多个订阅");
}
private Action<string> actions;

[Test]
public void RepeatCancelSubscribe()
{
bool exception = false;

try
[TearDown]
public void TearDown()
{
actions -= Say;
if (actions != null)
foreach (Action<string> a in actions.GetInvocationList())
actions -= a;
}
catch

[Test]
public void RepeatSubscribe()
{
exception = true;
actions += Say;
actions += Say;
actions += Say;

Debug.Log(actions.GetInvocationList().Length == 1 ? "重复订阅不会产生多个订阅" : "重复订阅会产生多个订阅");
}

Debug.Log(exception ? "重复取消订阅会导致异常" : "重复取消订阅会导致异常");
}
[Test]
public void RepeatCancelSubscribe()
{
bool exception = false;

[Test]
public void Subscribed()
{
bool subscribed = false;
actions += Say;
try
{
actions -= Say;
}
catch
{
exception = true;
}

foreach (Action<string> action in actions.GetInvocationList())
if (action == Say)
subscribed = true;
Debug.Log(exception ? "重复取消订阅会导致异常" : "重复取消订阅会导致异常");
}

Debug.Log(subscribed ? "使用 == 可以判断出是否已经订阅" : "使用 == 无法判断出是否已经订阅");
}
[Test]
public void Subscribed()
{
bool subscribed = false;
actions += Say;

private void Say(string str)
{
Debug.Log(str);
foreach (Action<string> action in actions.GetInvocationList())
if (action == Say)
subscribed = true;

Debug.Log(subscribed ? "使用 == 可以判断出是否已经订阅" : "使用 == 无法判断出是否已经订阅");
}

private void Say(string str)
{
Debug.Log(str);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
using NUnit.Framework;
using System.Linq;

namespace MtC.Tools.QuadtreeCollider {
namespace MtC.Tools.QuadtreeCollider.Test
{
[TestFixture]
public class ListTest
{
Expand Down
Loading

0 comments on commit f00f582

Please sign in to comment.