572734b5745d — Duncan Ross Palmer 1 year, 2 months ago
Write randomSymbol
2 files changed, 14 insertions(+), 23 deletions(-)

M bin/raw2c
M t/raw2c.pl
M bin/raw2c +7 -21
@@ 70,8 70,14 @@ sub randChar {
 	return substr($validChars, $randOffset, 1);
 }
 
+sub randomSymbol {
+	my $buffer = '';
+	$buffer .= randChar() foreach (0..19);
+	return '__rawimage_' . $buffer;
+}
+
 sub main {
-	srand($PID);
+	srand($PID + (time() % 100_000));
 	return EXIT_SUCCESS;
 }
 

          
@@ 270,26 276,6 @@ exit(Main->new->main()) unless (caller()
 #	return EXIT_SUCCESS;
 #}
 
-#static char *randomSymbol() {
-
-#	const char prefix[] = "__rawimage_";
-#	static char buffer[64];
-#	time_t currentTime;
-#	unsigned int offset;
-
-#	time(&currentTime);
-#	srand(currentTime);
-
-#	strcpy(buffer, prefix);
-
-#	for ( offset = sizeof(prefix)-1; offset < 20; offset++ )
-#		buffer[offset] = randChar();
-
-#	buffer[offset] = '\0';
-
-#	return buffer;
-#}
-
 #static void makeSource(struct FileOrDir *pIn, struct FileOrDir *pOut) {
 
 #	unsigned long int counter = 0UL;

          
M t/raw2c.pl +7 -2
@@ 37,9 37,9 @@ sub testStrings {
 	return EXIT_SUCCESS;
 }
 
-sub testRandChar {
+sub testRandom {
 	#my ($self) = @_;
-	plan tests => 1;
+	plan tests => 2;
 
 	my $sut = Main->new();
 

          
@@ 51,6 51,11 @@ sub testRandChar {
 
 	cmp_ok(scalar(keys(%chars)), '==', 4, 'randChar');
 
+	srand(0); # Predictable symbol
+	my $randomSymbol = $sut->randomSymbol();
+	my $expect = '__rawimage_aL6SANHnTKsmKgoMRAJ5';
+	is($randomSymbol, $expect, "Generated expected symbol: '$expect'");
+
 	return EXIT_SUCCESS;
 }