vendredi 25 septembre 2009

RDV du banc du 23/09/2009

Le 23/09/2009 a eu lieu le premier rendez vous du banc !!
Nous étions peu nombreux, 3 personnes, mais le plaisir n'en fut pas moindre !
Pour info nous étions 2 développeurs et 1 architecte.

Sujet proposé : Coding dojo IPhone.
Le but de ce dojo était de coder un convertisseur de chiffres romains en chiffres décimaux en TDD pour l'IPhone.
Le sujet de ce dojo est disponible ici

Voici un apercu du code source Objective-C issu de notre dojo

Tests unitaires (réalisés avec OCUnit)



   1:  #import "RomanToDecimal.h"
   2:   
   3:  @interface RomanToDecimalTest : SenTestCase
   4:  {
   5:      RomanToDecimal * romanToDecimal;
   6:  }
   7:   
   8:  - (void) assertConversion: (int) expectedInt : (NSString*) roman;
   9:   
  10:  @end
  11:      
  12:  @implementation RomanToDecimalTest
  13:   
  14:  - (void) setUp {
  15:      romanToDecimal = [[RomanToDecimal alloc] init];
  16:  }
  17:   
  18:  - (void) testCanConvertOneRomanSymbol {
  19:      [self assertConversion: 1    :@"I"];
  20:      [self assertConversion: 5    :@"V"];
  21:      [self assertConversion: 10   :@"X"];
  22:      [self assertConversion: 50   :@"L"];
  23:      [self assertConversion: 100  :@"C"];
  24:      [self assertConversion: 500  :@"D"];
  25:      [self assertConversion: 1000 :@"M"];
  26:  }
  27:   
  28:  - (void) testCanConvertIV {
  29:      [self assertConversion:4 :@"IV"];
  30:  }
  31:   
  32:  - (void) testCanConvertMMMCCXC {
  33:      [self assertConversion:3290 :@"MMMCCXC"];
  34:  }
  35:   
  36:   
  37:  - (void) assertConversion: (int) expectedInt : (NSString*) roman {
  38:      STAssertEquals(expectedInt, [romanToDecimal convert: roman], @"");
  39:  }

Implémentation



   1:  @interface RomanToDecimal : NSObject {
   2:      NSMutableDictionary *romanSymbol;
   3:  }
   4:   
   5:  - (int) convert: (NSString*) roman;
   6:  - (int) getDecimalValueAt: (int) index: (NSString*)roman;
   7:   
   8:  @end
   9:   
  10:  @implementation RomanToDecimal
  11:   
  12:  - (id) init
  13:  {
  14:      romanSymbol = [NSMutableDictionary dictionary];
  15:      NSArray *values = [[NSArray alloc] initWithObjects:
  16:                         [NSNumber numberWithInt:1],
  17:                         [NSNumber numberWithInt:5],
  18:                         [NSNumber numberWithInt:10],
  19:                         [NSNumber numberWithInt:50],
  20:                         [NSNumber numberWithInt:100],
  21:                         [NSNumber numberWithInt:500],
  22:                         [NSNumber numberWithInt:1000],
  23:                         nil ];
  24:      NSArray *keys = [[NSArray alloc] initWithObjects:
  25:                          @"I",
  26:                          @"V",
  27:                          @"X",
  28:                          @"L",
  29:                          @"C",
  30:                          @"D",
  31:                          @"M",
  32:                          nil];
  33:      romanSymbol = [NSDictionary dictionaryWithObjects:values forKeys:keys];
  34:      return self;
  35:  }
  36:   
  37:   
  38:  - (int) convert: (NSString*) roman {    
  39:      int decimalValue = 0;
  40:      int lastValue = INT_MIN;
  41:          
  42:      for (int i = [roman length]-1; i>=0; i--) {
  43:          int elementValue = [self getDecimalValueAt:i :roman];
  44:          if (elementValue < lastValue) {
  45:              decimalValue -= elementValue;
  46:          } else {
  47:              decimalValue += elementValue;
  48:          }
  49:          lastValue = elementValue;
  50:      }
  51:      
  52:      return decimalValue;
  53:  }
  54:   
  55:  - (int) getDecimalValueAt: (int) index: (NSString*)roman {
  56:      char romanChar = [roman characterAtIndex:index];
  57:      NSString *aRoman = [NSString stringWithFormat:@"%C", romanChar];
  58:      return [[romanSymbol objectForKey:aRoman] intValue];
  59:  }
  60:   
  61:  @end

Aucun commentaire:

Enregistrer un commentaire