这次的博客内容为,举例解释等价类划分。
1. 问题描述
EditBox
在文本输入框内输入文字,然后按确认键。
允许接收的文字为:1至6个英文字符或数字。
2.等价类划分
有效等价类 | 编号 | 无效等价类 | 编号 | |
包括的字符 | a-z,A-Z,0-9 | 1 | 其他字符 | 3 |
字符串长度 | 1-6 | 2 | 0 | 4 |
大于6 | 5 |
3.测试用例
编号 | 输入 | 覆盖等价类 | 预期输出 |
Test1 | a | 1,2 | Accepted |
Test2 | A | 1,2 | Accepted |
Test3 | 0 | 1,2 | Accepted |
Test4 | abAB01 | 1,2 | Accepted |
Test5 | 4 | Please try again. | |
Test6 | abcdefg | 5 | Please try again. |
Test7 | @ | 3 | Please try again. |
Test8 | a b | 3 | Please try again. |
Test9 | ab_cd | 3 | Please try again. |
4.代码实现及结果样例
使用C#编写,具体代码如下。
这是C#自动生成的,描述GUI的Form1.Designer.cs的代码:
namespace csharptest {partial class Form1{/// <summary>/// 必需的设计器变量。/// </summary>private System.ComponentModel.IContainer components = null;/// <summary>/// 清理所有正在使用的资源。/// </summary>/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region Windows 窗体设计器生成的代码/// <summary>/// 设计器支持所需的方法 - 不要/// 使用代码编辑器修改此方法的内容。/// </summary>private void InitializeComponent(){this.textBox1 = new System.Windows.Forms.TextBox();this.label1 = new System.Windows.Forms.Label();this.button1 = new System.Windows.Forms.Button();this.SuspendLayout();// // textBox1// this.textBox1.Location = new System.Drawing.Point(24, 49);this.textBox1.Name = "textBox1";this.textBox1.Size = new System.Drawing.Size(185, 21);this.textBox1.TabIndex = 0;// // label1// this.label1.AutoSize = true;this.label1.Location = new System.Drawing.Point(51, 19);this.label1.Name = "label1";this.label1.Size = new System.Drawing.Size(131, 12);this.label1.TabIndex = 1;this.label1.Text = "请输入0~6个字母或数字";// // button1// this.button1.Location = new System.Drawing.Point(72, 85);this.button1.Name = "button1";this.button1.Size = new System.Drawing.Size(75, 23);this.button1.TabIndex = 2;this.button1.Text = "确认";this.button1.UseVisualStyleBackColor = true;this.button1.Click += new System.EventHandler(this.button1_Click);// // Form1// this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.ClientSize = new System.Drawing.Size(227, 124);this.Controls.Add(this.button1);this.Controls.Add(this.label1);this.Controls.Add(this.textBox1);this.Name = "Form1";this.Text = "EditBox";this.ResumeLayout(false);this.PerformLayout();}#endregionprivate System.Windows.Forms.TextBox textBox1;private System.Windows.Forms.Label label1;private System.Windows.Forms.Button button1;} }
这是其他代码,有关于判断字符串是否合法的部分,文件名为Form1.cs:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;namespace csharptest {public partial class Form1 : Form{public Form1(){InitializeComponent();}private bool check(String text){if (text.Length <= 0) return false;if (text.Length > 6) return false;for (int i = 0; i < text.Length; i++){char c = text[i];if ((c < 'a' || c > 'z') && (c < 'A' || c > 'Z') && (c < '0' || c > '9'))return false;}return true;}private void button1_Click(object sender, EventArgs e){if (check(textBox1.Text))MessageBox.Show("Accepted");elseMessageBox.Show("Please try again.");}} }
测试用例结果图:
测试编号 | EditBox | 返回结果 |
Test1 | ||
Test2 | ||
Test3 | ||
Test4 | ||
Test5 | ||
Test6 | ||
Test7 | ||
Test8 | ||
Test9 |