Skip to content

Instantly share code, notes, and snippets.

@dimmaq
Created March 25, 2020 13:06
Show Gist options
  • Save dimmaq/cd8805424f5f08dd5eb3f712f709f440 to your computer and use it in GitHub Desktop.
Save dimmaq/cd8805424f5f08dd5eb3f712f709f440 to your computer and use it in GitHub Desktop.
Testing Challenge #4 - generate testing data
package main
/*
What to do
Create 5 valid data input for the specification below.
Enter them 1 at a time until you reach 5.
This challenges emphasizes the skill required to create your own test data.
Specification
Each person born in Romania receives a unique identification number.
The number has 13 digits, e.g. 1234567890123
What the digits represent:
First digit – the gender: male or female
1 or 2 – born between 1 January 1900 and 31 December 1999
3 or 4 - born between 1 January 1800 and 31 December 1899
5 or 6 - born between 1 January 2000 and 31 December 2099
7 or 8 – Foreign residents in Romania.
9 - For non-residents
Next 2 digits – last 2 digits of the year of birth (e.g. born in 1980 then it will 80 )
Next 2 digits – month of birth (01 to 12)
Next 2 digits - date of birth (01 to 31 depending on the month of birth)
Next 2 digits – area code (valid codes are 01 to 52)
Next 3 digits – order number
Last digit control number is created by:
Take the first 12 numbers of the CNP: 123456789012 and multiply each number with the corresponding position number from this string: 279146358279 like: 1 * 2 + 2 * 7 + 3 * 9 + 4 * 1+…..+ 12 * 9= X
You divide X by 11 the rest obtained if it is 10 then the last digit is 1 otherwise it equals the rest obtained
*/
import (
"fmt"
"math/rand"
"strconv"
)
func cnum(s string) string {
var NUMS = [12]int{ 2, 7, 9, 1, 4, 6, 3, 5, 8, 2, 7, 9}
// Take the first 12 numbers of the CNP: 123456789012 and multiply each number with the corresponding position number from this string:
// 279146358279 like: 1 * 2 + 2 * 7 + 3 * 9 + 4 * 1+…..+ 12 * 9= X
// You divide X by 11 the rest obtained if it is 10 then the last digit is 1 otherwise it equals the rest obtained
r := 0
for i, ch := range s {
k := int(ch) - 48
if (k < 0) || (k > 9) {
continue
}
r += NUMS[i] * k
// fmt.Println(i+1, "*" , k, "=", (i + 1) * k, "/", r)
}
k := r % 11
cnt := k
if cnt == 10 {
cnt = 1
}
// fmt.Println(r, k, cnt)
return strconv.Itoa(cnt)
}
func randrange(a, b, z int) string {
if a > b {
b, a = a, b
}
k := rand.Intn(b - a + 1) + a
if z > 0 {
return fmt.Sprintf("%0"+strconv.Itoa(z)+"d", k)
}
return strconv.Itoa(k)
}
func makeiud() string {
// First digit – the gender: male or female
gender := randrange(1, 2, 0)
// last 2 digits of the year of birth
year := randrange(80, 99, 2)
// month of birth (01 to 12)
month := randrange(1, 12, 2)
// date of birth (01 to 31 depending on the month of birth)
day := randrange(1, 29, 2)
// Next 2 digits – area code (valid codes are 01 to 52)
area := randrange(1, 52, 2)
// Next 3 digits – order number
order := randrange(1, 999, 3)
//
// fmt.Println(gender, year, month, day, area, order)
//
return gender + year + month + day + area + order
}
func test(s string) {
fmt.Println(s + cnum(s))
// fmt.Println("---------------------------")
}
func main() {
rand.Seed(42);
const COUNT = 7
// test("187082443706")
for i := 1; i <= COUNT; i++ {
test(makeiud())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment