问题描述
我的main.cs代码:
My main.cs code:
public string Generate(int length)
{
char[] chars = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&".ToCharArray();
string password = string.Empty;
Random random = new Random();
for (int i = 0; i < length; i++)
{
int x = random.Next(1, chars.Length);
if (!password.Contains(chars.GetValue(x).ToString()))
password += chars.GetValue(x);
else
i--;
}
return password;
}
我有一个测试代码
[TestMethod]
[Timeout(1000)]
public void RenderingPasswordShouldHaveMaximumSize()
{
var amountOfCharacters = Int32.MaxValue;
var generator = new PasswordGenerator();
var target = generator.Generate(amountOfCharacters);
Assert.Fail("This method should throw an exception if you try to create a password with too many characters");
}
但它给我以下错误:
我这个?我的密码最大为74个。
Can someone help me with this? My password needs to have a max size of 74.
推荐答案
我刚刚注意到你确实需要你的测试时间,忽略部分 Int32.MaxValue
。
I've just noticed you actually want your test to take a long time, ignore the part about Int32.MaxValue
.
你的循环有点奇怪,你递减迭代器为了尝试和利用所有的字母,但这可能会导致问题。
Your loop is a little strange, you're decrementing the iterator in order to try and utilise all of the letters, but that can cause problems. It's not a case of it taking too long, but of it not really generating a password properly.
我会修复您的版本 像这样:
static string Generate(int length)
{
// also, seed your random so you don't get the same password
Random random = new Random((int)DateTime.Now.Ticks);
char[] chars = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&".ToCharArray();
string password = string.Empty;
for (int i = 0; i < chars.Length; i++)
{
int x = random.Next(0, chars.Length);
if (!password.Contains(chars.GetValue(x).ToString()))
password += chars.GetValue(x);
else
i--;
}
if (length < password.Length) password = password.Substring(0, length);
return password;
}
我已经测试过了,它的工作原理:
I've tested this and it works:
测试代码:
Debug.WriteLine(Generate(5));
Thread.Sleep(50);
Debug.WriteLine(Generate(10));
Thread.Sleep(50);
Debug.WriteLine(Generate(20));
Thread.Sleep(50);
Debug.WriteLine(Generate(30));
Thread.Sleep(50);
Debug.WriteLine(Generate(40));
Thread.Sleep(50);
Debug.WriteLine(Generate(60));
Thread.Sleep(50);
结果:
SDxF0
i8ZLhm1gxn
@0Ldn7I&1:Kg2x3SYE;m
U?5uO%N4hkpq1*y;9SRVaer^Eij:bT
nvL;E3#D1MQgTicSdojHOwz:VFk2x&94a*PZ@X80
cSm39n:%1sL*lk2B?yVDTPZCA0vGb@udjeW5KoUw6qRNxY^pazH$JXiQ;tFg
另外,请记住,在添加字符到密码的方式,你使它使用 char [] 但没有重复。这意味着您的密码的最大长度等于您的
char []
的长度。
Also, bear in mind that the way you are adding characters to the password, you're making it so that it utilises all characters in the
char[]
but has no repeats. What this means is that the max length of your password is equal to the length of your char[]
.
我建议这样:
static string Generate(int length)
{
Random random = new Random((int)DateTime.Now.Ticks);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++)
{
int x = random.Next(33, 123);
sb.Append((char)x);
}
return sb.ToString();
}
这给出相同的结果,但也可以处理更长的长度,例如200 :
Which gives the same results, but can also handle longer lengths, such as 200:
t5[5l
WoEZG;8^9<
6(4Q*Y7k>`?ohte6F^pe
DVb\5l^JMKc`q&[#$U4Kq^\OW`JrRi
#iQSq0\WRoDe<]k36nOhNOb-#"Zt!cK8iaR.I>VF
1<%^"B)6bZhWEiazmfmO7Vv*Rw]TbKV+GRJUc%k23Lq/HC.I6Eha6!5V@v-&
jOnV_`(Cf$I\kFr/%%/loG"bx9lV1E?`[A"y1)pF5tA".x?**(E/>kzCN-XGnL2B`c!JEtxw0cXu'zTztcM*z0CkBYK%LIRcbz<Fxo`0*HzU4&=NTXM,z)CP@1)fJtKa2o3'_/#&#=zR1iW<<wLnwFdYXg&hgta0x:C?m6jEVH*])k@QTgKMIXdg,UJr*59)VkUrG8J&
这篇关于测试执行超过超时周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!