Monday, April 23, 2007

test class for baseball program MyProject

// The following code was generated by Microsoft Visual Studio 2005.
// The test owner should check each test for validity.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Text;
using System.Collections.Generic;
using MyProject;
namespace TestMyProject
{
///
///This is a test class for MyProject.Form1 and is intended
///to contain all MyProject.Form1 Unit Tests
///

[TestClass()]
public class Form1Test
{


private TestContext testContextInstance;

///
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///

public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes
//
//You can use the following additional attributes as you write your tests:
//
//Use ClassInitialize to run code before running the first test in the class
//
//[ClassInitialize()]
//public static void MyClassInitialize(TestContext testContext)
//{
//}
//
//Use ClassCleanup to run code after all tests in a class have run
//
//[ClassCleanup()]
//public static void MyClassCleanup()
//{
//}
//
//Use TestInitialize to run code before running each test
//
//[TestInitialize()]
//public void MyTestInitialize()
//{
//}
//
//Use TestCleanup to run code after each test has run
//
//[TestCleanup()]
//public void MyTestCleanup()
//{
//}
//
#endregion


///
///A test for Form1 ()
///

[TestMethod()]
public void ConstructorTest()
{
Form1 target = new Form1();

// TODO: Implement code to verify target
Assert.AreEqual(target.Basepath.Count, 3);
}


///
///A test for oneBagger ()
///

[TestMethod()]
public void oneBaggerTest()
{
Form1 target = new Form1();

target.oneBagger();

Assert.AreEqual(target.Basepath.Pop(), 1);

Assert.AreEqual(target.Basepath.Pop(), 0);


}

///
///A test for twoBagger ()
///

[TestMethod()]
public void twoBaggerTest()
{
Form1 target = new Form1();

target.oneBagger();

target.twoBagger();

Assert.AreEqual(target.Basepath.Count, 8);
}

///
///A test for threeBagger ()
///

[TestMethod()]
public void threeBaggerTest()
{
Form1 target = new Form1();
target.oneBagger();
target.twoBagger();
target.threeBagger();


Assert.AreEqual(target.Basepath.Count, 11);

Assert.AreEqual(target.Basepath.Pop(), 0);

Assert.AreEqual(target.Basepath.Pop(), 0);

Assert.AreEqual(target.Basepath.Pop(), 1);




}

///
///A test for homer ()
///

[TestMethod()]
public void homerTest()
{
Form1 target = new Form1();

target.homer();

Assert.AreEqual(target.Basepath.Count, 7);

Assert.AreEqual(target.Basepath.Pop(), 0);

Assert.AreEqual(target.Basepath.Pop(), 0);

Assert.AreEqual(target.Basepath.Pop(), 0);


}

///
///A test for walk ()
///

[TestMethod()]
public void walkTest()
{
Form1 target = new Form1();

target.threeBagger();
target.walk();
Assert.AreEqual(target.Basepath.Count, 6);
target.walk();
Assert.AreEqual(target.Basepath.Count, 6);
target.walk();
Assert.AreEqual(target.Basepath.Count, 7);

}

///
///A test for addRuns ()
///

[TestMethod()]
public void addRunsTest()
{
Form1 target = new Form1();



target.threeBagger();
target.walk();
Assert.AreEqual(target.Basepath.Count, 6);
target.walk();
Assert.AreEqual(target.Basepath.Count, 6);
target.walk();
Assert.AreEqual(target.Basepath.Count, 7);

target.twoBagger();

target.addRuns();



Assert.AreEqual(target.Runs, 4);
}

///
///A test for strikeout ()
///

[TestMethod()]
public void strikeoutTest()
{
Form1 target = new Form1();

target.strikeout();
target.strikeout();
target.walk();
target.walk();
target.walk();
target.strikeout();
target.homer();
target.strikeout();
target.strikeout();



target.addRuns();



Assert.AreEqual(target.Runs, 1);




}

///
///A test for bat ()
///

[TestMethod()]
public void batTest()
{
Form1 target = new Form1();

for (int i = 0; i < 28; i++ )
target.bat();

target.addRuns();

Assert.IsTrue(target.Runs < 21);
Assert.IsTrue(target.Runs >= 0);


}
}


}

Sunday, April 22, 2007

Perl one-liner for text replacement

Perl one-liners contain a single command terminated with a semi-colon, followed by a list of the files that it should operate on.

Here is an example, which removes a reference to a file name 1169048439 inside a file:


perl -wpl -e 's/1169048439\///g;' 1169048439

The above will only print to the screen. If you want to replace the file with the corrected file one way to do it is to say

perl -wpl -e 's/1149008916\///g;' 1149008916 > tempo

and then

mv tempo 1149008916

Writing the output directly on top of the modified file creates a blank file in Centos 4

Thursday, April 19, 2007

C# Baseball Simulation Program

// The following program assumes a Form called Form1 which has three checkboxes
// and two buttons and two labels. All objects should use their default names
// and there should have been no other objects on the form.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MyProject
{
public partial class Form1 : Form
{
int runs = 0;
int outs = 0;
int innings = 0;
Random RandomClass = new Random();

//A basepath is a string of zeroes and ones. The first three elements are the three bases.
//A one means the base is occupied. A zero means it is empty.
//Any 1s beyond the first three elements represent runners who scored.
//Zeroes beyond the first three elements can be ignored.

Stack basepath = new Stack();

int[] intArray;





public Form1()
{
InitializeComponent();
//create the three bases and set them to empty.
basepath.Push(0);
basepath.Push(0);
basepath.Push(0);
}


public void oneBagger()
{
//advance runners two bases.
basepath.Push(0);
basepath.Push(1);

}


public void twoBagger()
{
//advance runners three bases.
basepath.Push(0);
basepath.Push(1);
basepath.Push(0);

}

public void threeBagger()
{
basepath.Push(1);
basepath.Push(0);
basepath.Push(0);

}

public void homer()
{
basepath.Push(1);
basepath.Push(0);
basepath.Push(0);
basepath.Push(0);
}

public void walk()
{
// look for first empty base and
// place a runner there.
// if all bases are occupied move all runners forward one base.
if (basepath.Pop() == 0)
basepath.Push(1);
else if (basepath.Pop() == 0)
{
basepath.Push(1);
basepath.Push(1);
}
else if (basepath.Pop() == 0)
{
basepath.Push(1);
basepath.Push(1);
basepath.Push(1);
}
else
{
basepath.Push(1);
basepath.Push(1);
basepath.Push(1);
basepath.Push(1);
}
}

public void addRuns()
{
//for use at end of game.
//Throw away baserunners since they can never score.
basepath.Pop();
basepath.Pop();
basepath.Pop();
intArray = basepath.ToArray();
for (int i = 0; i < intArray.GetLength(0); i++)
{
if (intArray[i] == 1)
{
runs++;
}
}

label1.Text = runs.ToString();

}

public void strikeout()
{
outs = outs + 1;
label2.Text = outs.ToString();
if (outs > 2)
{
// end of inning;
outs = 0;
label2.Text = outs.ToString();
innings = innings + 1;
basepath.Pop();
basepath.Pop();
basepath.Pop();
basepath.Push(0);
basepath.Push(0);
basepath.Push(0);
checkBox1.Checked = false;
checkBox2.Checked = false;
checkBox3.Checked = false;
}

if (innings > 8)
label2.Text = "Game Over";

}

public void bat()
{
double num = RandomClass.NextDouble();
if (label2.Text == "Game Over")
{
label1.Text = "Press For Totals";
}
else if (num < .225)
{
oneBagger();
}
else if (num < .275)
{
twoBagger();
}
else if (num < .300)
{
threeBagger();
}

else if (num < .325)
{
homer();
}
else if (num < .400)
{
walk();
}

else
{
strikeout();
}

intArray = basepath.ToArray();

// set checkboxes based upon whether bases are occupied.
if (intArray[0] == 1)
{
checkBox1.Checked = true;
}
else
{
checkBox1.Checked = false;
}


if (intArray[1] == 1)
{
checkBox2.Checked = true;
}
else
{
checkBox2.Checked = false;
}


if (intArray[2] == 1)
{
checkBox3.Checked = true;
}
else
{
checkBox3.Checked = false;
}




}



private void button1_Click(object sender, EventArgs e)
{

addRuns();
}

private void button2_Click(object sender, EventArgs e)
{

bat();



}

private void label1_Click(object sender, EventArgs e)
{

}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{

}
}
}

Thursday, April 05, 2007

To find Squishdot pages with file uploads google "Click To Download Attachment"