public static class Generator { public static readonly Random RND = new Random(); // Generate IP address public static string GetIP(bool isIPv6 = false) { if (isIPv6) { return string.Format("{0}.{1}.{2}.{3}.{4}.{5}", RND.Next(0,256), RND.Next(0,256), RND.Next(0,256), RND.Next(0,256), RND.Next(0,256), RND.Next(0,256)); } return string.Format("{0}.{1}.{2}.{3}", RND.Next(0,256), RND.Next(0,256), RND.Next(0,256), RND.Next(0,256)); } // Generate GPS coordinate public static System.Device.Location.GeoCoordinate GetGeo() { return new System.Device.Location.GeoCoordinate { Latitude = GetDouble(90), Longitude = GetDouble(90), }; } // Generate string public static string GetString(int size, int percentOfSymbols = 30, int percentOfDigits = 30) { StringBuilder builder = new StringBuilder(); int[] symbol = new[] { 33, 36, 38, 64, 94 }; int[] digits = new[] { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57 }; int symbolsAmount = (int) size * percentOfSymbols /100; int digitAmount = (int)size * percentOfDigits / 100; for (int i = 0; i < size; i++) { int switcher = RND.Next(3); char ch; if (switcher == 0 && symbolsAmount > 0) { int id = RND.Next(symbol.Length); ch = Convert.ToChar(symbol[id]); symbolsAmount--; } else if (switcher == 1 && digitAmount > 0) { int id = RND.Next(digits.Length); ch = Convert.ToChar(digits[id]); digitAmount--; } else { bool isLower = RND.Next(2) == 0 ? false : true; ch = Convert.ToChar( Convert.ToInt32( Math.Floor(26 * RND.NextDouble() + ((isLower) ? 65 : 97)) ) ); } builder.Append(ch); } return builder.ToString(); } // Generate Date public static DateTime GetDate() { return DateTime.Now.AddMinutes( RND.Next(10000000) * (RND.Next() % 2 == 0 ? -1 : 1)); } //Get double public static double GetDouble(int limit = default(int)) { return double.Parse(string.Format("{0}.{1}", limit == default(int) ? RND.Next() : RND.Next(90), RND.Next())) * (RND.Next() % 2 == 0 ? -1 : 1); } }
Wednesday, January 02, 2013
Test value generator
Frequently in the test time required to set random values to variables. The simple class will help you to save a time in the future:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment