"Software Test" experiment two unit test

Source: Internet
Author: User

1. (1) Write a program that parses the frequency of occurrences of each word in a string and displays the word and the frequency at which it appears. (the words are separated by a space, such as "Hello World My First Unit Test");

Import Java.util.HashMap;

Import Java.util.Iterator;

Import Java.util.Map;

Import Java.util.Set;

public class Demo {

public void Wordscount (String str) {

string[] Wordsarray = Str.split ("");

map<string, integer> wordsmap = new hashmap<string, integer> ();

for (String Word:wordsarray) {

if (Wordsmap.containskey (word)) {

Wordsmap.put (Word, wordsmap.get (word) + 1);

}

else {

Wordsmap.put (Word, 1);

}

}

set<string> Setkey = Wordsmap.keyset ();

iterator<string> Itkey = Setkey.iterator ();

while (Itkey.hasnext ()) {

String Word = Itkey.next (). toString ();

int count = Wordsmap.get (word);

System.out.println ("Word:" + Word + "appears" + Count + "Times");

}

}

}

(2) Writing unit tests for testing;

Import static org.junit.assert.*;

Import Org.junit.Test;

public class Demotest {

@Test

public void Test () {

Demo Demo = new Demo ();

Demo.wordscount ("Hello This is A test test");

}

}

(3) Use Elcemma to view code coverage, requiring coverage to reach 100%.

2. (1) The order of words in an English sentence is reversed and then output. For example, enter "How IS is", output "you is how";

public class Demo {
public void Upsidedown (String str) {
string[] Wordsarray = Str.split ("");
System.out.print ("Reverse word output:");
for (int i = wordsarray.length-1; I >= 0; i--) {
System.out.print (Wordsarray[i] + "");
}
}
}

(2) Writing unit tests for testing;

Import static org.junit.assert.*;
Import Org.junit.Test;
public class Demotest {
@Test
public void Test () {
Demo Demo = new Demo ();
Demo.upsidedown ("Hello This is A test test");
}
}

(3) Use Elcemma to view code coverage, requiring coverage to reach 100%.

"Software Test" experiment two unit test

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.