Introduction to Sets and Logic • Why do we need to know a bit of set theory? – Set theory allows us to describe and classify entities • So it allows us to describe and group the data that we may need to use for testing – Set theory allows us to operate on groups of entities • So it allows us to operate on groupings of test data – Set theory allows us to extend the study to relations and functions • So it allows us to relate test data or test scenarios and expected results • Why do we need to know a bit about logic? – Propositional logic allows us to manipulate statements and truth values of the statements; predicate logic allows us to introduce quantifiers such as “there exists” or “all” • So it allows us to describe and combine test conditions and deduce validity of test coverage or validity of test results. Set theory and logic, while useful for all aspects of testing, it is most useful with functional testing (black box testing) What is a set ? • A set is a collection or a group of something. – For example : a set of items in a classroom may be represented as follows. • Classrooom_items = { chair1, chair2, desk1, student 1, student2, desk2, desk3, chair3, instructor, whiteboard, computer} • Student_ages = {18, 17, 23, 19} – A word of caution: a set can not have repetition. • If you had two students of the same age, then you may be tempted to say: incorrect – Student_ages = { 17, 18, 18, 23} but in set theory you can only say: correct – Student_ages = {17, 18, 23} Different Ways to Represent a Set • A) Listing the elements that belong to the set, just as shown earlier: – my_lucky_ numbers = {3, 106, 7689, -4} • B) Providing a decision rule that defines the set – possible_male_ages = { age | 0 < age ≤ 200 } • C) Combinations of or redefinition of sets – students_in_class = boy_students U girl_students There is a special set called the empty set which contains no element. It is represented with the symbol, Ø. For example the set, possible_male_age = { age| age ≤ -3}, is an empty set. So this set is Ø. Common Set Operations • Union : union of set A and set B is the combination of sets A and B, represented as A U B. If A= {apple, orange, grapes} and B = {shoe, tie, shirt}, then AUB = {apple, orange, grapes, shoe, tie, shirt} • Intersection: intersection of set A and B is the set whose elements are composed of only the common elements from set A and set B, represented as A ∩ B. If A and B are as defined above, then A∩B = Ø If A = { apple, grape, orange} and B = { tomato, grape, lettuce}, then A∩B = {grape} • Complement: complement of a set A is the set that has all the elements that are not in A, represented as A’ or ¬A If A = { numbers | numbers > 0 }, then ¬A = {numbers| numbers ≤ 0 } Some more Set Operations • Minus: set A minus set B is the set which contains all elements in A that is not in B, represented as (A – B). this set is sometimes known as “relative complement” of B with respect to A. – If A = {2, 34, 67, 98, -3, -54, -300} and B = { integers | integers ≤ 0}, then (A – B) = {2,34,67,98} • Symmetric Difference: the symmetric difference of sets A and B, represented as A + B, is a set that is defined as the result of operations, {A U B} – { A ∩ B} – If A = { apple, orange, grape} and B = { pear, grape, orange}, then A + B = {apple, pear} Specification vs Implementation vs Test Cases Specification Implementation Actual Expected 2 3 1 5 4 Look at this earlier Venn Diagram The area represented by “number 2” may be represented as [{Expected} ∩{Actual} ] – {Tested} 6 7 Tested Test Cases A few More Definitions • Set A is a subset of set B, written A B, if every element in A is also an element in B. • Set A is a proper subset of set B, written A B, if it is a subset of B and there exists at least one element in B that is not in A (or B – A ≠ Ø). • Set A and set B are equal, written A = B, if every element in A is in B and vise versa. Set Partition • A partitioning of a set is a division of a set into groups. This is a useful concept for many areas where we want to take samples from groups formed based on some criteria. – Example: performing an opinion survey of teenagers on clothes. We may partition the survey by: • Age groups - {13 to 15} , {16 to 17}, {18 to 19} • Gender - { male} , {female} • Types of schools - {Catholic}, {Jewish}, {Baptist}, {non-religious public} • This concept is important when we have to design test cases based on picking the input data. • Formally, a dividing of a set X into subset x1, x2, - - - xn is called a partitioning of X if the following is true: a) b) x1 U x2 U - - - U xn = X for any xi and xk , where i ≠ k , xi ∩ xk = Ø For later: Discuss - partitioning the outputs of the “triangle” problem for testing Some “Rules” about sets • AUØ=A • • • • A∩Ø=Ø (A’)’ = A Commutative rules: A U B = B U A; A ∩ B = B ∩ A Associative rules: A U (B U C) = (A U B) U C A ∩ (B ∩ C) = (A ∩ B) ∩ C • Distributive rules : A ∩ (B U C) = (A ∩ B) U (A ∩ C) A U (B ∩ C) = (A U B) ∩ (A U C) • DeMorgan’s rules: ¬(A U B) = ¬A ∩ ¬B ¬(A ∩ B) = ¬A U ¬B Informally - Relation • A Relation is, as the English word says, a “connection” between two sets. • Given a set A = {Tom, Sam, Sally, Jane} and a set B = (Joe, Mary, Abe}, then a “relation” such as “ parent of” may be defined between these set B and A. If Joe is the father of Sam and Mary is the mother of Tom, then the relation, B “parent of” A, is true with some of the elements. • For the above sets of A and B, there may or may not be any “meaningful” relation. Relationship between Two Sets SWE_students Good_Programs written by The arrows represent the relationship, “written by.” Then one can see that 3 of the Good_Programs are written by 2 of the SWE students. Relation (more formally) • Given two sets A and B, the cross-product of A x B is the set composed of paired items, where first element of the pair is from A and the second element of the pair is from B. – Example : A = {3. 4, 8} and B = {mod_x, mod_y} then crossproduct of A x B = { (3, mod_x), (4, mod_x), (8,mod_x), (3, mod_y), (4, mod_y) , (8, mod_y) } • Given a set A and a set B, a relation, R, is a subset of cross-product of A and B. So R AxB. – Example : A = { tom, mary} and B = {abu, kathy}, A x B = { (tom, abu), (tom, kathy), (mary, abu), (mary, kathy) }. If kathy is the sister of tom, then a relation, R, called “brother of” is a subset of A x B where “brother of” = { (tom, kathy) } Some Properties of Relation • A relation, R1, defined over a set A, is said to be reflexive if for every a in set A, (a,a) is in R1. – Example: If A is set of positive integers and R1 is the “=“ relation, then R1 is reflexive because every positive integer = itself. But if R1 were defined as >, then R1 is not reflexive. • A relation, R2, is said to be symmetric if for every (a,b) in R2, (b,a) is also inR2. – Example: if for (a,b) in R2 means a is b’s classmate, then b is also a’s classmate. Thus (b,a) is also in R2. But if R2 is defined as (a,b) mean a is b’s father, the R2 is not symmetric. • A relation, R3, is said to be transitive if for every (a.b) and (b,c) in R3 implies that (a,c) is in R3. – Example: If R3 is defined as “>”, then (7,4) and (4,2) in R3 implies that (7,2) is also in R3 because 7 > 2. – Example: R3 defined as “mother of” relation is not transitive There is a relation called equivalence relation which is reflexive, symmetric, and transitive. We will use the partitioning of test data into subsets that are each considered to be an equivalence class and pick a test case from each class. Function • Function is a special type of relation. • A relation, RF, is a function if for every (x,y) in RF and (x,z) in RF implies that y= z. RF is a non-function Is the inverse, RF-1, a function? RF is a function Is the inverse, RF-1, a function? Simple Logic: Propositional Calculus • A proposition is a statement. • A proposition, just like a statement, carries the value of True or False, but not both. – Example: Let X be the proposition, “Howard is a student in SWE 3643.” Then X is either True or False. – Example: Let Y be the proposition, “It will rain tomorrow.” Is Y True or False ? Note that “it will rain tomorrow” is not a proposition that can be handled by propositional calculus because its truth vale can not determined until tomorrow. Basic Operators of Propositional Calculus • Let P and Q be propositions, then the truth values of 5 logical operators (NOT,AND, OR, Exclusive OR, Implies) is shown in the following truth table. PΛq P + q p q ¬q T T F T T F T T F T F T T F F T F F T T T F F T F F F T Pvq P→ q Legend: ¬ is NOT ; Λ is logical AND; v is logical OR ; + is logical Exclusive OR ; → is logical Implies. Logical Implies • All the truth operations are quite intuitive, except for “implies,” also known as “if --then” – P implies Q intuitively says that when P is true then Q must be true – It is also intuitive that when P is true then Q can not be false. – What happens when P is NOT True? • Propositional Calculus defines this situation as True regardless of the truth value of Q. – Sometimes “P implies Q,” P →Q, is stated as { (P AND Q ) OR (NOT P) }. Look at the previous truth table for (PΛ Q) OR ( ¬P) Some Logical Expression Rules • Simple ones: – P Λ True = P – P v False = P – P v True = True – P Λ False = False – ¬ (¬ P) = P – PΛQ=QΛP – PvQ =QvP • Associative : (P v Q) v R = P v (Q v R) ; ( P Λ Q) Λ R = P Λ (Q Λ R) • Distributive: P Λ (Q v R) = (P Λ Q) v (P Λ R) ; P v (Q Λ R) = (P v Q) Λ (P v R) • Demorgan’s Law : ¬ (P Λ Q) = ¬P v ¬Q ¬ (P v Q) = ¬P Λ ¬Q Use the truth table to prove to yourself one of the DeMorgan’s Law