import java.util.*;
/**
* Created by Mridul on 9/25/2015.
*/
public class DotcomSuiteGenerator {
/**
* method which recursively generates the probable testcases
* @param prefix
* @param lists
*/
private static int counter;
private List<String> randomList=new ArrayList<>();
private List<String> copyOfRandomList=new ArrayList<>();
/* public void generate(String prefix, String spacer, List<String>... lists) {
int len=lists.length;
if(len > 0) {
List<String> list = lists[0];
lists = Arrays.copyOfRange(lists, 1, len);
for (String value : list) {
String name = prefix + value + spacer;
generate(name,spacer,lists); //recursive call
if(lists.length==0) {
System.out.println(name.substring(0,name.length()-1));
++counter;
}
}
}
}*/
public void generate(String prefix, String spacer, List<String>... lists) {
int len=lists.length;
if(len > 0) {
List<String> list = lists[0];
lists = Arrays.copyOfRange(lists, 1, len);
for (String value : list) {
String name = prefix + value + spacer;
generate(name,spacer,lists); //recursive call
if(lists.length==0) {
name = name + getRandomListValue();
System.out.println(name);
++counter;
}
}
}
}
private String getRandomListValue(){
String result="";
int size=copyOfRandomList.size();
if(size==0)
copyOfRandomList=new ArrayList<>(randomList);
if( size >= 0) {
Collections.shuffle(copyOfRandomList);
result = copyOfRandomList.remove(0);
}
return result;
}
public void appendRandomVariables(List<String>... lists) {
//creating combo list containing all the data for booking
List<String> comboBookingParams=new ArrayList<>();
for(int index=0; index < lists.length; index++) {
comboBookingParams.addAll(lists[index]);
}
randomList=comboBookingParams;
copyOfRandomList=new ArrayList<>(randomList);
for(String value: copyOfRandomList){
System.out.println("value : "+value);
}
System.out.println(" copyOfRandomList.size() : "+copyOfRandomList.size());
}
public static void main(String... args){
//constants
//String prefix="Shopping_Calendar_Matrix_Pricing_"; //for shopping
String prefix="Booking_";
String spacer="_";
//parameters
List<String> tripTypeOW=Arrays.asList("OW");
List<String> tripType=Arrays.asList("RT","MC");
List<String> route=Arrays.asList("DOM","INTL");
List<String> paxTypeADT= Arrays.asList("ADT");
List<String> paxTypeSRC= Arrays.asList("SRC");
List<String> payTypeD=Arrays.asList("Dollars");
List<String> payTypeP=Arrays.asList("Points");
List<String> flightType=Arrays.asList("Dir","Conn");
List<String> fareType=Arrays.asList("BUS","ANY","WGA");
List<String> fareTypeRED=Arrays.asList("BUSRED","ANYRED","WGARED");
List<String> scenarioType=Arrays.asList("SameMonth","DifferentMonth");
List<String> discountType=Arrays.asList("AmtDiscount","PercDiscount");
//booking specific parameters
List<String> creditCards=Arrays.asList("Visa","Mastercard","Diner","Discover","UATP","Amex");
List<String> otherPayTypes=Arrays.asList("GIF","LUV");
List<String> comboPayType=Arrays.asList("Visa_GIF","Visa_LUV","Mastercard_GIF","Mastercard_LUV",
"Diner_GIF","Diner_LUV","Discover_GIF","Discover_LUV",
"UATP_GIF","UATP_LUV","Amex_GIF","Amex_LUV");
List<String> cash=Arrays.asList("Cash");
DotcomSuiteGenerator instance=new DotcomSuiteGenerator();
System.out.println("The combination of tests are: ");
/*
ALL VALID COMBINATIONS OF DATA ARE GIVEN BELOW FOR SHOPPING
*/
//generates all Adult Dollars for OW
/* instance.generate(prefix, spacer, tripTypeOW, route, paxTypeADT, payTypeD, flightType, fareType, scenarioType);
//generates all Adult Points for OW
instance.generate(prefix, spacer, tripTypeOW, route, paxTypeADT, payTypeP, flightType, fareTypeRED, scenarioType);
//generates all Adult Dollars for RT and MC
instance.generate(prefix, spacer, tripType, route, paxTypeADT, payTypeD, flightType, flightType, fareType, scenarioType);
//generates all Adult Points for for RT and MC
instance.generate(prefix, spacer, tripType, route, paxTypeADT, payTypeP, flightType, flightType, fareTypeRED, scenarioType);
//generates all Senior Dollars for OW
instance.generate(prefix, spacer, tripTypeOW, route, paxTypeSRC, payTypeD, flightType, fareType, scenarioType);
//generates all Senior Points for OW
instance.generate(prefix, spacer, tripTypeOW, route, paxTypeSRC, payTypeP, flightType, fareTypeRED, scenarioType);
//generates all Senior Dollars for RT and MC
instance.generate(prefix, spacer, tripType, route, paxTypeSRC, payTypeD, flightType, flightType, fareType, scenarioType);
//generates all Senior Points for RT and MC
instance.generate(prefix, spacer, tripType, route, paxTypeSRC, payTypeP, flightType, flightType, fareTypeRED, scenarioType);*/
//ALL PERCENTAGE AND AMOUNT DISCOUNT SCENARIOS- NOT IN EXCEL SHEET- FOR FUTURE USE
/* //generates all Adult Dollars with Amount and Percentage Discount
instance.generate(prefix, spacer, tripType, route, paxTypeADT, payTypeD, flightType, fareType, scenarioType, discountType);
//generates all Adult Points with Percentage Discount
instance.generate(prefix, spacer, tripType, route, paxTypeADT, payTypeP, flightType, fareTypeRED, scenarioType, Arrays.asList("PercDiscount"));
//generates all Senior Dollars with Amount and Percentage Discount
instance.generate(prefix, spacer, tripType, route, paxTypeSRC, payTypeD, flightType, fareType, scenarioType, discountType);
//generates all Senior Points with Percentage Discount
instance.generate(prefix, spacer, tripType, route, paxTypeSRC, payTypeP, flightType, fareTypeRED, scenarioType, Arrays.asList("PercDiscount"));*/
//EXPERIMENTAL- FOR FUTURE TESTCASES WITH DIFFERENT FARE TYPES ON FLIGHT SEGMENTS OF RT AND MC TESTCASES
/* //generates all RT and MC trips for ADT with Dollars with separate segments
instance.generate(prefix, spacer, Arrays.asList("RT","MC"), route, paxTypeADT, payTypeD, flightType, fareType, fareType, scenarioType);*/
//generate other combo types-one time run
//instance.generate("", spacer, creditCards, otherPayTypes);
/*
ALL VALID COMBINATIONS OF DATA ARE GIVEN BELOW FOR BOOKING
*/
//SET ALL APPENDABLE LISTS HERE
instance.appendRandomVariables(creditCards, otherPayTypes, comboPayType, cash);
//generates all Adult Dollars for OW
instance.generate(prefix, spacer, tripTypeOW, route, paxTypeADT, payTypeD, flightType, fareType, scenarioType);
//generates all Adult Points for OW
instance.generate(prefix, spacer, tripTypeOW, route, paxTypeADT, payTypeP, flightType, fareTypeRED, scenarioType);
//generates all Adult Dollars for RT and MC
instance.generate(prefix, spacer, tripType, route, paxTypeADT, payTypeD, flightType, flightType, fareType, scenarioType);
//generates all Adult Points for for RT and MC
instance.generate(prefix, spacer, tripType, route, paxTypeADT, payTypeP, flightType, flightType, fareTypeRED, scenarioType);
//generates all Senior Dollars for OW
instance.generate(prefix, spacer, tripTypeOW, route, paxTypeSRC, payTypeD, flightType, fareType, scenarioType);
//generates all Senior Points for OW
instance.generate(prefix, spacer, tripTypeOW, route, paxTypeSRC, payTypeP, flightType, fareTypeRED, scenarioType);
//generates all Senior Dollars for RT and MC
instance.generate(prefix, spacer, tripType, route, paxTypeSRC, payTypeD, flightType, flightType, fareType, scenarioType);
//generates all Senior Points for RT and MC
instance.generate(prefix, spacer, tripType, route, paxTypeSRC, payTypeP, flightType, flightType, fareTypeRED, scenarioType);
System.out.println("The total number of tests is : "+counter);
Monday, November 2, 2015
Code to generate test cases and add a uniform distribution of parameters
Sunday, October 18, 2015
Cows and Bulls Game
import java.util.*;
/**
* Created by Mridul on 10/17/15.
*/
public class CowsAndBulls {
private final int[] secretNumber;
private int[] guessNumber;
private List<String> guesses;
private int size;
private static final int DEFAULT_SIZE=4;
private int getSize(){
if(size==0) {
return DEFAULT_SIZE;
}else{
return size;
}
}
private void setSize(int size){
this.size = size;
}
public int[] getSecretNumber(){
return secretNumber;
}
CowsAndBulls(){
secretNumber=populateSecretNumber();
guessNumber=new int [getSize()];
Arrays.fill(guessNumber,0);
guesses=new ArrayList<>();
}
private int[] populateSecretNumber(){
Random rand=new Random();
int size=getSize();
int[] number= new int[size];
for(int i=0;i<size;i++){
number[i]= rand.nextInt(9);
}
return number;
}
public void verifyGuess(String guess){
try {
int iguess = Integer.parseInt(guess);
int len = guess.length();
int remainder = 0;
while (len > 0) {
guessNumber[--len] = iguess % 10;
iguess = iguess / 10;
}
computeCowsAndBulls();
}catch (NumberFormatException nfe){
System.out.println("Please enter a valid number");
}
}
private void computeCowsAndBulls(){
int cows=0;
int bulls=0;
for(int i=0; i<guessNumber.length;i++){
for(int j=0;j<secretNumber.length;j++){
if(guessNumber[i]==secretNumber[j]){
//if indexes are same then bulls
if(i==j) {
++bulls;
}else {
++cows;
}
}
}
}
System.out.printf("%d bulls and %d cows\n", bulls, cows);
}
public static void main(String... args){
CowsAndBulls instance =new CowsAndBulls();
do {
System.out.println("Starting Cows and Bulls game!");
System.out.println("Please type EXIT to quit the game anytime!");
System.out.println("Please type REVEAL to show the 4 digit number anytime!");
System.out.println("Please start guessing the 4 digit number : ");
Scanner scanner = new Scanner(System.in);
String currentGuess = scanner.next();
if(currentGuess.equalsIgnoreCase("EXIT")){
System.out.println("Thanks for playing the game!");
System.exit(0);
}else if(currentGuess.equalsIgnoreCase("REVEAL")){
System.out.println("The Secret number is : "
+ Arrays.toString(instance.getSecretNumber()));
System.out.println("Thanks for playing the game!");
System.exit(0);
}
else {
instance.verifyGuess(currentGuess);
}
}while(true);
}
}
Friday, September 25, 2015
Code to generate a combination of strings recursively-Used to generate test cases from parameters
import java.util.*; /** * Created by x217018 on 9/25/2015. */ public class DotcomSuiteGenerator { /** * method which recursively generates the probable testcases * @param prefix * @param lists */ public void generate(String prefix, String spacer, List<String>... lists) { int len=lists.length; if(len > 0) { List<String> list = lists[0]; lists = Arrays.copyOfRange(lists, 1, lists.length); for (String value : list) { String name = prefix + value + spacer; generate(name,spacer,lists); //recursive call if(lists.length==0) { System.out.println(name.substring(0,name.length()-1)); } } } } public static void main(String... args){ //constants String prefix="Shopping_Calendar_Matrix_Pricing_"; String spacer="_"; //parameters List<String> tripType=Arrays.asList("OW","RT","MC"); List<String> route=Arrays.asList("DOM","INTL"); List<String> paxTypeADT= Arrays.asList("ADT"); List<String> paxTypeSRC= Arrays.asList("SRC"); List<String> payType=Arrays.asList("Dollars","Points"); List<String> flightType=Arrays.asList("Dir","Conn"); List<String> fareTypeADT=Arrays.asList("BUS","ANY","WGA"); List<String> fareTypeSRC=Arrays.asList("BUSRED","ANYRED","WGARED"); List<String> scenarioType=Arrays.asList("SameMonth","DifferentMonth"); List<String> discountType=Arrays.asList("AmtDiscount","PercDiscount"); DotcomSuiteGenerator instance=new DotcomSuiteGenerator(); System.out.println("The combination of tests are: "); //generates all Adult testcases instance.generate(prefix, spacer, tripType, route, paxTypeADT, payType, flightType, fareTypeADT, scenarioType); //generates all Senior testcases instance.generate(prefix, spacer, tripType, route,paxTypeSRC,payType,flightType,fareTypeSRC,scenarioType); //generates all Adult testcases with Dollars with both Amount and Percentage Discount //instance.generate(prefix, spacer, tripType, route,paxTypeADT,Arrays.asList("Dollars"),flightType,fareTypeADT,scenarioType, discountType); //generates all Seniors testcases with Points with both Percentage Discount //instance.generate(prefix, spacer, tripType, route,paxTypeADT,Arrays.asList("Points"),flightType,fareTypeADT,scenarioType,Arrays.asList("PercDiscount")); } } //Sample data /* The combination of tests are: Shopping_Calendar_Matrix_Pricing_OW_DOM_ADT_Dollars_Dir_BUS_SameMonth Shopping_Calendar_Matrix_Pricing_OW_DOM_ADT_Dollars_Dir_BUS_DifferentMonth Shopping_Calendar_Matrix_Pricing_OW_DOM_ADT_Dollars_Dir_ANY_SameMonth Shopping_Calendar_Matrix_Pricing_OW_DOM_ADT_Dollars_Dir_ANY_DifferentMonth Shopping_Calendar_Matrix_Pricing_OW_DOM_ADT_Dollars_Dir_WGA_SameMonth Shopping_Calendar_Matrix_Pricing_OW_DOM_ADT_Dollars_Dir_WGA_DifferentMonth */
Monday, April 13, 2015
Javascript: Program to see if a given number can be reached by repeatedly adding 5 or multiplying by 3
/* Program to see if a number can be obtained
by repeatedly adding 5 or multiplying by 3 */
function evaluateCandidate(candidate){
var counter=0;
var operationsPerformed="";
while(candidate>1) {
if(candidate%3==0) {
operationsPerformed+=(++counter)+". candidate/3\n";
candidate=candidate/3;
}else{
operationsPerformed+=(++counter)+". candidate-5\n";
candidate=candidate-5;
}
}
if(candidate==1) {
return operationsPerformed;
}else{
return "Invalid candidate";
}
}
evaluateCandidate(24);
Subscribe to:
Posts (Atom)