public void QuoteAll_False() { String tmpFile = System.IO.Path.GetTempFileName(); this.testFiles.Add(tmpFile); int rowCount = Rando.RandomInt(50, 100); var ht = new Hashtable(); ht.Add("one", "Sentence"); ht.Add("two", "Domain"); ht.Add("three", "Name"); ps.AddCommand("New-RandomCSVFile") .AddParameter("RowCount", rowCount) .AddParameter("IncludeHeader", true) .AddParameter("QuoteAll", false) .AddParameter("Columns", ht) .AddParameter("OutputFile", tmpFile); var list = this.DoInvoke <FileInfo>(); Assert.AreEqual(1, list.Count); VerifyCSVFile(tmpFile, ",", rowCount + 1, null, false); }
public void WordTest() { String tmpFile = System.IO.Path.GetTempFileName(); this.testFiles.Add(tmpFile); long size = Rando.RandomInt(500, 5000); ps.AddCommand("New-RandomFile") .AddParameter("Size", size) .AddParameter("OutputFile", tmpFile) .AddParameter("StringType", "Word") .AddParameter("Seperator", ";"); var list = this.DoInvoke <FileInfo>(); Assert.AreEqual(1, list.Count); foreach (var hr in list) { Assert.IsTrue(hr.Length >= size, String.Format("Output size is smaller than requested: expected={0}, actual={1}", size, hr.Length)); Assert.AreEqual(tmpFile, hr.FullName); Char[] text = System.IO.File.ReadAllText(hr.FullName).ToCharArray(); for (int i = 0; i < text.Length; i++) { Char c = text[i]; Assert.IsTrue((Char.IsLetter(c) || c == ';' || c == ' ' || c == '-' || c == '\''), String.Format("Character is not an expected value: '{0}',{1}", c, i)); } } }
public void DigitOnlyTest() { String tmpFile = System.IO.Path.GetTempFileName(); this.testFiles.Add(tmpFile); long size = Rando.RandomInt(500, 5000); ps.AddCommand("New-RandomFile") .AddParameter("Size", size) .AddParameter("OutputFile", tmpFile) .AddParameter("StringType", "Digits"); var list = this.DoInvoke <FileInfo>(); Assert.AreEqual(1, list.Count); foreach (var hr in list) { Assert.AreEqual(size, hr.Length); Assert.AreEqual(tmpFile, hr.FullName); Char[] text = System.IO.File.ReadAllText(hr.FullName).ToCharArray(); for (int i = 0; i < text.Length; i++) { Char c = text[i]; Assert.IsTrue(Char.IsDigit(c), String.Format("Character is not a digit: '{0}',{1}", c, i)); } } }
private string newObject(int currentDepth) { StringBuilder sb = new StringBuilder(); if (currentDepth > this.MaxDepth) { return("{}"); } sb.Append("{"); int numItems = Rando.RandomInt(1, this.MaxWidth); for (int i = 0; i < numItems; i++) { String key = newKey(); String value = newValue(currentDepth + 1); sb.AppendFormat("\"{0}\":{1}", key, value); if (i + 1 < numItems) { sb.Append(","); } } sb.Append("}"); return(sb.ToString()); }
private static String CombineNameForEmail(String firstname, String lastname) { String ans; var i = Rando.RandomInt(0, 10); switch (i) { case 0: ans = firstname + lastname; break; case 1: ans = firstname + "_" + lastname; break; case 2: ans = firstname + lastname[0]; break; case 3: ans = firstname[0] + lastname; break; case 4: ans = lastname + firstname[0] + firstname[1]; break; default: ans = firstname + "." + lastname; break; } return(ans.Replace("'", "")); }
private String[] getKeyList() { List <String> ans = new List <String>(); if (this.Columns == null || this.Columns.Count == 0) { this.Columns = new Hashtable(); int count = Rando.RandomInt(3, 15); for (int i = 0; i < count; i++) { String key = StringGenerator.GetString(StringType.Word); String type = Rando.RandomPick <String>(new String[] { "AaZz:10", "Digits:5", "Hex:8", "EmailSimple:5", "Domain", "Name", "Word", "IPAddress", "DateTime:yyyy-MM-ddTHH:mm:ss" }); ans.Add(key); this.Columns.Add(key, type); } } else { foreach (var key in this.Columns.Keys) { ans.Add((String)key); } } return(ans.ToArray()); }
public void Example002() { String tmpFile = System.IO.Path.GetTempFileName(); this.testFiles.Add(tmpFile); int rowCount = Rando.RandomInt(1000, 100000); var ht = new Hashtable(); ht.Add("one", "IPv4"); ht.Add("two", "Word"); ht.Add("three", "EmailSimple:10"); ht.Add("four", "Hex:5"); ht.Add("five", "datetime:yyyy-MM-ddTHH:mm:ss"); ps.AddCommand("New-RandomCSVFile") .AddParameter("RowCount", rowCount) .AddParameter("IncludeHeader", true) .AddParameter("Columns", ht) .AddParameter("OutputFile", tmpFile); var list = this.DoInvoke <FileInfo>(); Assert.AreEqual(1, list.Count); VerifyCSVFile(tmpFile, ",", rowCount + 1, 5, null); }
public String Encode(String input) { //1. extract input into parts int start = 0; List <String> parts = new List <String>(); while (start < input.Length) { int pLen = Rando.RandomInt(this.MinPartLength, this.MaxPartLength); if (start + pLen >= input.Length) { parts.Add(input.Substring(start)); } else { parts.Add(input.Substring(start, pLen)); } start += pLen; } //2. shuffle order of parts String[] list = parts.ToArray(); int[] order; this.Shuffle(ref list, out order); //3. return concatenation of parts and ordering in given language return(LanguageFactory.EncodeToStringReorder(Language.Powershell, list, order)); }
private string newValue(int currentDepth) { if (currentDepth > this.MaxDepth) { return(""); } string value = null; String type = Rando.RandomPick <String>(new String[] { "string", "string", "number", "number", "boolean", "datetime", "array", "object" }); switch (type) { case "string": value = StringGenerator.GetString(StringType.Word); break; case "number": value = Rando.RandomInt(int.MinValue, int.MaxValue).ToString(); break; case "boolean": value = Rando.RandomBoolean().ToString().ToLower(); break; case "datetime": DateTime dt = DateTime.Now .AddYears(Rando.RandomInt(-100, 100)) .AddMonths(Rando.RandomInt(-11, 11)) .AddDays(Rando.RandomInt(-30, 30)) .AddHours(Rando.RandomInt(-23, 23)) .AddMinutes(Rando.RandomInt(-59, 59)) .AddSeconds(Rando.RandomInt(-59, 59)); value = dt.ToString("yyyy-MM-ddTH:mm:ss.fffK"); break; case "array": string key = newKey(); StringBuilder sb = new StringBuilder(); sb.AppendFormat("<{0}>", key); int numItems = Rando.RandomInt(1, this.MaxWidth); string childKey = newKey(); for (int i = 0; i < numItems; i++) { sb.AppendFormat("<{0}>{1}</{0}>", childKey, newValue(currentDepth + 1)); } sb.AppendFormat("</{0}>", key); value = sb.ToString(); break; case "object": value = newObject(currentDepth + 1); break; } return(value); }
private string newValue(int currentDepth) { if (currentDepth > this.MaxDepth) { return("\"" + StringGenerator.GetString(StringType.Word) + "\""); } string value = null; String type = Rando.RandomPick <String>(new String[] { "string", "string", "number", "number", "boolean", "datetime", "array", "object" }); switch (type) { case "string": value = "\"" + StringGenerator.GetString(StringType.Word) + "\""; break; case "number": value = Rando.RandomInt(int.MinValue, int.MaxValue).ToString(); break; case "boolean": value = Rando.RandomBoolean().ToString().ToLower(); break; case "datetime": DateTime dt = DateTime.Now .AddYears(Rando.RandomInt(-100, 100)) .AddMonths(Rando.RandomInt(-11, 11)) .AddDays(Rando.RandomInt(-30, 30)) .AddHours(Rando.RandomInt(-23, 23)) .AddMinutes(Rando.RandomInt(-59, 59)) .AddSeconds(Rando.RandomInt(-59, 59)); value = "\"" + dt.ToString("yyyy-MM-ddTH:mm:ss.fffK") + "\""; break; case "array": value = "["; int numItems = Rando.RandomInt(1, this.MaxWidth); for (int i = 0; i < numItems; i++) { value += newValue(currentDepth + 1); if (i + 1 < numItems) { value += ","; } } value += "]"; break; case "object": value = newObject(currentDepth + 1); break; } return(value); }
public void Example001() { String tmpFile = System.IO.Path.GetTempFileName(); this.testFiles.Add(tmpFile); int rowCount = Rando.RandomInt(5, 25); ps.AddCommand("New-RandomCSVFile") .AddParameter("RowCount", rowCount) .AddParameter("OutputFile", tmpFile); var list = this.DoInvoke <FileInfo>(); Assert.AreEqual(1, list.Count); VerifyCSVFile(tmpFile, ",", rowCount + 1, null, null); }
private static String GenerateEmail(Person p) { String ans; var user = CombineNameForEmail(p.GivenName, p.SurName); var n = Rando.RandomInt(1, 5); var commonTLD = new String[] { "com", "com", "com", "com", "com", "com", "com", "com", "com", "com", "com", "com", "net", "org", "edu", "org", "edu" }; switch (n) { case 1: ans = user + "@" + MadLibHelper.madlib.Generate("[adjective][noun]") + "." + StringGenerator.GetString(StringType.TLD); break; default: ans = user + "@" + MadLibHelper.madlib.Generate("[adjective][noun]") + "." + commonTLD.GetRandomItem(); break; } return(ans.ToLowerInvariant()); }
public void BasicAlgoVerify() { int width = Rando.RandomInt(10, 512); int height = Rando.RandomInt(10, 512); ps.AddCommand("New-RandomBitmap") .AddParameter("Width", width) .AddParameter("Height", height); var list = this.DoInvoke <Bitmap>(); Assert.AreEqual(1, list.Count); foreach (var hr in list) { Assert.AreEqual(width, hr.Width); Assert.AreEqual(height, hr.Height); hr.Dispose(); } }
static String GetRandomItem(String key) { String ans = null; String modifer = null; if (key.Contains(":")) { String[] parts = key.SplitAtFirst(':'); key = parts[0]; modifer = parts[1]; } if (replacements.ContainsKey(key)) { int idx = Rando.RandomInt(0, replacements[key].Length); ans = replacements[key][idx]; if (ans.StartsWith("[") && ans.EndsWith("]")) { ans = GetRandomItem(ans.Substring(1, ans.Length - 2).ToUpperInvariant()); } } if (!String.IsNullOrWhiteSpace(modifer)) { switch (modifer.ToUpperInvariant()) { case "TITLECASE": ans = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(ans); break; case "UPPERCASE": ans = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToUpper(ans); break; case "LOWERCASE": ans = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToLower(ans); break; } } return(ans); }
private string newObject(int currentDepth) { StringBuilder sb = new StringBuilder(); if (currentDepth > this.MaxDepth) { return(""); } string objName = newKey(); sb.AppendFormat("<{0}>", objName); int numItems = Rando.RandomInt(1, this.MaxWidth); for (int i = 0; i < numItems; i++) { String key = newKey(); String value = newValue(currentDepth + 1); sb.AppendFormat("<{0}>{1}</{0}>", key, value); } sb.AppendFormat("</{0}>", objName); return(sb.ToString()); }
private static String GenerateUri() { //<scheme name> : <hierarchical part> [ ? <query> ] [ # <fragment> ] StringBuilder sb = new StringBuilder(); #region Schemes //Taken from perm list at http://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml on 2014-09-17 (last updated: 2014-09-16) String[] schemes = new String[] { "aaa", "aaas", "about", "acap", "acct", "cap", "cid", "coap", "coaps", "crid", "data", "dav", "dict", "dns", "file", "ftp", "geo", "go", "gopher", "h323", "http", "https", "iax", "icap", "im", "imap", "info", "ipp", "iris", "iris.beep", "iris.xpc", "iris.xpcs", "iris.lwz", "jabber", "ldap", "mailto", "mid", "msrp", "msrps", "mtqp", "mupdate", "news", "nfs", "ni", "nih", "nntp", "opaquelocktoken", "pop", "pres", "reload", "rtsp", "rtsps", "rtspu", "service", "session", "shttp", "sieve", "sip", "sips", "sms", "snmp", "soap.beep", "soap.beeps", "stun", "stuns", "tag", "tel", "telnet", "tftp", "thismessage", "tn3270", "tip", "turn", "turns", "tv", "urn", "vemmi", "ws", "wss", "xcon", "xcon-userid", "xmlrpc.beep", "xmlrpc.beeps", "xmpp", "z39.50r", "z39.50s" }; #endregion sb.Append(Rando.RandomPick(schemes)); sb.Append(@"://"); if (Rando.RandomInt(0, 20) % 20 == 0) //1 in 20 chance { // UserInfo sb.Append(GetString(StringType.Word)); sb.Append(":"); sb.Append(GetString(StringType.Word)); } if (Rando.RandomInt(0, 5) % 5 == 0) //1 in 5 chance { //Subdomain int num = Rando.RandomInt(1, 5); for (int i = 0; i < num; i++) { sb.Append(GetString(StringType.Word)); sb.Append("."); } } //Domain sb.Append(StringGenerator.GetString(StringType.Word)); sb.Append("."); sb.Append(StringGenerator.GetString(StringType.TLD)); if (Rando.RandomBoolean()) { // Path int num = Rando.RandomInt(1, 5); for (int i = 0; i < num; i++) { sb.Append(@"/"); sb.Append(GetString(StringType.Word)); } } if (Rando.RandomBoolean()) { // File sb.Append(GetString(StringType.Word)); sb.Append("."); sb.Append(GetString(StringType.AlphaNumeric, (ulong)Rando.RandomInt(3, 5))); } if (Rando.RandomInt(0, 3) % 3 == 0) //1 in 3 chance { // Query int num = Rando.RandomInt(1, 10); sb.Append("?"); for (int i = 0; i < num; i++) { if (i > 0) { sb.Append("&"); } sb.Append(GetString(StringType.Word)); sb.Append("="); sb.Append(GetString(StringType.Word)); } } if (Rando.RandomInt(0, 5) % 5 == 0) //1 in 5 chance { // Fragment sb.Append("#"); sb.Append(GetString(StringType.Word)); } return(sb.ToString()); }
/// <summary> /// ProcessRecord - primary Cmdlet func /// </summary> protected override void ProcessRecord() { bool numColumns = this.Columns == null; using (var writer = new StreamWriter(this.OutputFile)) { String[] keys = getKeyList(); if (this.IncludeHeader) { writer.WriteLine(String.Join(this.Seperator.ToString(), keys)); } for (ulong row = 0; row < this.RowCount; row++) { for (int col = 0; col < keys.Length; col++) { if (col != 0) { writer.Write(this.Seperator); } var key = keys[col]; var val = (String)this.Columns[key]; String[] valParts = val.SplitAtFirst(':'); uint length = 0; if (valParts.Length > 1) { val = valParts[0]; uint.TryParse(valParts[1], out length); } StringType st; if (Enum.TryParse <StringType>(val, out st)) { if (length > 0) { val = StringGenerator.GetString(st, length); } else { val = StringGenerator.GetString(st); } } else { switch (val.ToLowerInvariant()) { case "datetime": DateTime dt = DateTime.Now .AddYears(Rando.RandomInt(1, 1)) .AddMonths(Rando.RandomInt(-11, 11)) .AddDays(Rando.RandomInt(-30, 30)) .AddHours(Rando.RandomInt(-23, 23)) .AddMinutes(Rando.RandomInt(-59, 59)) .AddSeconds(Rando.RandomInt(-59, 59)); val = dt.ToString(valParts[1]); break; } } if (this.QuoteAll.HasValue) { if (this.QuoteAll.Value) { writer.Write("\""); writer.Write(val); writer.Write("\""); } else { writer.Write(val); } } else { if (val.Contains(" ") || val.Contains(this.Seperator)) { writer.Write("\""); writer.Write(val); writer.Write("\""); } else { writer.Write(val); } } } writer.WriteLine(); } } if (numColumns) { this.Columns = null; } WriteObject(new FileInfo(this.OutputFile)); }
public static PointF[] GeneratePoints(Shapes shape, RectangleF bounds) { List <PointF> ans = new List <PointF>(); float cx = bounds.X + (bounds.Width / 2); float cy = bounds.Y + (bounds.Height / 2); float radius = Math.Min(bounds.Width, bounds.Height) / 2; switch (shape) { case Shapes.Rectangle: case Shapes.Square: ans.Add(new PointF(bounds.X, bounds.Y)); ans.Add(new PointF(bounds.X + bounds.Width, bounds.Y)); ans.Add(new PointF(bounds.X + bounds.Width, bounds.Y + bounds.Height)); ans.Add(new PointF(bounds.X, bounds.Y + bounds.Height)); ans.Add(new PointF(bounds.X, bounds.Y)); break; case Shapes.Diamond: ans.Add(new PointF(cx, cy - radius)); ans.Add(new PointF(cx + radius, cy)); ans.Add(new PointF(cx, cy + radius)); ans.Add(new PointF(cx - radius, cy)); break; case Shapes.Triangle_Right: ans.Add(new PointF(bounds.X, bounds.Y)); ans.Add(new PointF(bounds.X + bounds.Width, bounds.Y + bounds.Height)); ans.Add(new PointF(bounds.X, bounds.Y + bounds.Height)); ans.Add(new PointF(bounds.X, bounds.Y)); break; case Shapes.Triangle_CenterTop: ans.Add(new PointF(cx, bounds.Y)); ans.Add(new PointF(bounds.X + bounds.Width, bounds.Y + bounds.Height)); ans.Add(new PointF(bounds.X, bounds.Y + bounds.Height)); ans.Add(new PointF(cx, bounds.Y)); break; case Shapes.Triangle_CenterBottom: ans.Add(new PointF(cx, bounds.Y + bounds.Height)); ans.Add(new PointF(bounds.X, bounds.Y)); ans.Add(new PointF(bounds.X + bounds.Width, bounds.Y)); ans.Add(new PointF(cx, bounds.Y + bounds.Height)); break; case Shapes.Triangle_CenterLeft: ans.Add(new PointF(bounds.X, bounds.Y)); ans.Add(new PointF(bounds.X + bounds.Width, cy)); ans.Add(new PointF(bounds.X, bounds.Y + bounds.Height)); ans.Add(new PointF(bounds.X, bounds.Y)); break; case Shapes.Triangle_CenterRight: ans.Add(new PointF(bounds.X, cy)); ans.Add(new PointF(bounds.X + bounds.Width, bounds.Y)); ans.Add(new PointF(bounds.X + bounds.Width, bounds.Y + bounds.Height)); ans.Add(new PointF(bounds.X, cy)); break; case Shapes.RandomPolygon: { int count = Rando.RandomInt(3, 10); for (int i = 0; i < count; i++) { float x, y; Boolean isXBorder = Rando.RandomBoolean(); if (isXBorder) { if (Rando.RandomBoolean()) { x = bounds.X; } else { x = bounds.X + bounds.Width; } y = Rando.RandomFloat(bounds.Y, bounds.Y + bounds.Height); } else { x = Rando.RandomFloat(bounds.X, bounds.X + bounds.Width); if (Rando.RandomBoolean()) { y = bounds.Y; } else { y = bounds.Y + bounds.Height; } } ans.Add(new PointF(x, y)); } } break; default: throw new NotSupportedException(String.Format("Generate Points does not support: {0}", shape.ToString())); } return(ans.ToArray()); }
public static String GetString(StringType type, ulong length) { String ans = null; String charset = null; Boolean useMadLib = false; switch (type) { case StringType.AaZz: case StringType.Digits: case StringType.AlphaNumeric: case StringType.ANSI: case StringType.ASCII: case StringType.Hex: case StringType.UpperCase: case StringType.LowerCase: charset = GetCharSet(type); break; case StringType.Unicode: case StringType.Random: ans = GetUnicodeString(length); break; case StringType.EmailSimple: ans = GetString(StringType.Name, length).Replace(' ', '.') + "@" + GetString(StringType.Domain, length); ans = ans.ToLowerInvariant(); break; case StringType.Email: ans = GetString(StringType.ASCII, length) + "@" + GetString(StringType.Domain, length); break; case StringType.Domain: String tld = "." + GetString(StringType.TLD); ulong l = (Int64)length < (Int64)tld.Length ? 3 : length - (ulong)tld.Length; if (l < 3) { l = 3; } ans = GetString(StringType.AaZz, l) + tld; ans = ans.ConvertWhitespaceToSpaces().Replace(" ", ""); break; case StringType.TLD: ans = GetTLD(); break; case StringType.Name: useMadLib = true; charset = Rando.RandomBoolean() ? "[boyname]" : "[girlname]"; charset += " [lastname]"; break; case StringType.Word: useMadLib = true; charset = "[top5000]"; break; case StringType.Sentence: useMadLib = true; charset = GetSimpleSentenceStructure(); break; case StringType.Uri: ans = GenerateUri(); break; case StringType.IPAddress: if (Rando.RandomBoolean()) { ans = GetString(StringType.IPv4); } else { ans = GetString(StringType.IPv6); } break; case StringType.IPv4: ans = (new System.Net.IPAddress((long)Rando.RandomInt(0, int.MaxValue))).ToString(); break; case StringType.IPv6: { StringBuilder sb = new StringBuilder(); for (int i = 0; i < 8; i++) { if (i != 0) { sb.Append(":"); } if (!Rando.RandomBoolean(10)) { sb.Append(GetString(StringType.Hex, 4)); } } ans = sb.ToString(); } break; } if (useMadLib) { var m = new MadLib(); ans = m.Generate(charset); } else if (!String.IsNullOrEmpty(charset)) { StringBuilder sb = new StringBuilder(); for (ulong i = 0; i < length; i++) { sb.Append(Rando.RandomPick(charset)); } ans = sb.ToString(); } return(ans); }