PageRenderTime 23ms CodeModel.GetById 19ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/specs/runtime/switch.ds

http://github.com/wilkie/djehuty
Unknown | 122 lines | 104 code | 18 blank | 0 comment | 0 complexity | d93b72cd7716b0723c6cf8e2dd4de07c MD5 | raw file
  1module specs.runtime.switch;
  2
  3import core.util;
  4
  5import math.random;
  6
  7describe runtime() {
  8	describe _d_switch_string {
  9		it should_handle_empty_switch_statements {
 10			string foo = "hello";
 11
 12			switch(foo) {
 13				default:
 14					break;
 15			}
 16		}
 17
 18		it should_handle_one_case {
 19			string foo = "hello";
 20
 21			switch(foo) {
 22				case "hello":
 23					should(true);
 24					break;
 25				default:
 26					should(false);
 27					break;
 28			}
 29		}
 30
 31		it should_handle_three_cases {
 32			string foo = "hello";
 33
 34			switch(foo) {
 35				case "abc":
 36				case "zzt":
 37					should(false);
 38					break;
 39				case "hello":
 40					should(true);
 41					break;
 42				default:
 43					should(false);
 44					break;
 45			}
 46		}
 47
 48		template StringList(int idx) {
 49			static if (idx == 50) {
 50				const char[] StringList = `"` ~ IntToStr!(idx, 16) ~ `"
 51					`;
 52			}
 53			else {
 54				const char[] StringList = `"` ~ IntToStr!(idx, 16) ~ `",
 55					` ~ StringList!(idx+1);
 56			}
 57		}
 58
 59		template StringArray() {
 60			const char[] StringArray = `
 61				string[] foo = [
 62				` ~ StringList!(0) ~ `
 63				];`;	
 64		}
 65
 66		template CaseList(int idx) {
 67			static if (idx == 50) {
 68				const char[] CaseList = `case "` ~ IntToStr!(idx, 16) ~ `":
 69					picked = "` ~ IntToStr!(idx, 16) ~ `";
 70					break;`;
 71			}
 72			else {
 73				const char[] CaseList = `case "` ~ IntToStr!(idx, 16) ~ `":
 74					picked = "` ~ IntToStr!(idx, 16) ~ `";
 75					break;
 76				` ~ CaseList!(idx+1);
 77			}
 78		}
 79
 80		template SwitchList() {
 81			const char[] SwitchList = `
 82				switch(foo[idx]) {
 83					` ~ CaseList!(0) ~ `
 84					default:
 85						picked = "";
 86						break;
 87				}`;
 88		}
 89
 90		it should_handle_many_cases {
 91
 92			mixin(StringArray!());
 93
 94			auto r = new Random();
 95			for (size_t i=0; i<50; i++) {
 96				size_t idx = cast(size_t)r.nextLong(50);
 97				string picked;
 98
 99				mixin(SwitchList!());
100
101				should(picked == foo[idx]);
102			}
103		}
104
105		it should_handle_empty_string {
106			switch("") {
107				case "":
108					should(true);
109					break;
110				case "abc":
111				case "zsdf":
112				case "asdf":
113				case "afsfdfas":
114					should(false);
115					break;
116				default:
117					should(false);
118					break;
119			}
120		}
121	}
122}